My Photo

Microsoft Dynamics 365

Official Blog for Microsoft Dynamics Functional and Technical Solutions....
Browsing Category " D365 WebApi v9 "

Dynamics CRM V9 / D365 Connection through Console without Prompt

There are 2 major steps involved in the process.
  • Register the application with your Azure subscription
  • Build the Console Application
Register the application with your Azure subscription

Build the Console Application 

You need to change only the below authentication method in step 3 rest remains same

Pass user credentials with service URL and client id in Authentication Context


string authority = "https://login.microsoftonline.com/68e86d2-65ed-4666-8ee-f8ce245d20de/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority, false);
var credentials = new UserCredential("shashank@s.onmicrosoft.com", "pas@wrd1");
//without prompt
AuthenticationResult authResult = authContext.AcquireToken(serviceUrl, clientId, credentials);
authHeader = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
You can have a look at my code here :Sample Code

Happy Coding and CRMing 😊


SOURCE : mscrm.com

Get Plugin Registration tool and assemblies from NuGet Packages for Dynamics 365 , v9.0

With the release of Dynamics 365 (online), version 9.0, there are a lot of changes in our developer documentation:
No more monolithic SDK download : Getting the assemblies and tools you need
SDK assemblies and tools will be distributed only via NuGet. We will provide a script that will allow you to download the assemblies and tools from NuGet.

You can follow below steps to download Nuget Packages using Visual studio

Step 1 Right Click on to your visual studio project and click on to manage NuGet Packages


Step 2 Search for "Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool" and click on install


Click on I accept


Step 3 Navigate to the package directory of that project


Step 4 Search the folder Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.X.0.0.X



Done!! now you will be having your latest Plugin registration Tool

Sample PRT Reference for quick start  : Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.0.0.7

In the same way you can download any of the NuGet assemblies and tools

Hope it helps !!

Happy CRMing


SOURCE : mscrm.com

Create and Send Email Message Activity from javascript using api D365 / MS CRM 2016

For Creating email message activity from java script MS CRM , you can update and use the below code according to your requirement. Here to show I have taken up a scenario to send a email to system user when a Quote is won.

Replace the User GUID for to and from user in "fromFieldIdSystemAdmin" and  "toPartyUsersId"

function CreateEmail() {
    var fromFieldIdSystemAdmin = "53536B3D-4D71-4FC2-857C-0477750A92BE"; //todo Email from User GUID
    var toPartyUsersId = ["53536B3D-4D71-4FC2-857C-0477750A92BE"]; //todo Email To user array
    var quoteId = Xrm.Page.data.entity.getId();
    quoteId = quoteId.replace(/[{}]/g, "");
    var customerLookUp = Xrm.Page.getAttribute("customerid");
    var accountName = null;
    if (customerLookUp != null)
        accountName = customerLookUp.getValue()[0].name;
    var webapiPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/emails";
    var email = {};
    //Lookup
    email["regardingobjectid_quote@odata.bind"] = "/quotes(" + quoteId + ")"; //Regarding is quote
    email["subject"] = "Quote Sent ";
    email["description"] = "See attachment for the Quote that was set as Won";
    var parties = [];
    //ActivityParty (From)
    var sender = {};
    sender["partyid_systemuser@odata.bind"] = "/systemusers(" + fromFieldIdSystemAdmin + ")";
    sender["participationtypemask"] = 1; //From
    parties.push(sender);
    for (i = 0; i < toPartyUsersId.length; i++) {
        var receiver = {};
        receiver["partyid_systemuser@odata.bind"] = "/systemusers(" + toPartyUsersId[i] + ")";
        receiver["participationtypemask"] = 2; //To
        parties.push(receiver);
    }
    email["email_activity_parties"] = parties;
    var service = new XMLHttpRequest();
    service.open("POST", webapiPath, false);
    service.setRequestHeader("Accept", "application/json");
    service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    service.setRequestHeader("OData-MaxVersion", "4.0");
    service.setRequestHeader("OData-Version", "4.0");
    service.send(JSON.stringify(email));
    if (service.readyState == 4) {
        if (service.status == 204) {
            var uri = service.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var emailGblId = matches[1];
            SendEmail(emailGblId);
        } else {

            Xrm.Utility.alertDialog(this.statusText);
        }
    }
}

function SendEmail(emailGblId) {
    var parameters = {};
    parameters.IssueSend = true;
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/emails(" + emailGblId + ")/Microsoft.Dynamics.CRM.SendEmail", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));
}

function successCallback() {
}
function errorCallback() {
}
Happy Coding 😊


SOURCE : mscrm.com

Connect to Dynamics CRM / D365 WebApi v9 from Console Application C#

There are 2 major steps involved in the process.
  • Register the application with your Azure subscription
  • Build the Console Application
Prerequisites

CRM / D365 Trial Organisation

Register the application with your Azure subscription


Step 1 Login to azure portal  https://portal.azure.com with your CRM credentials


Step 2 Click on Azure Active Directory - App registrations


 Step 3 Click on to New application registration


 Step 4 Enter Your Desired Name and Redirect URL but select Native in Application type and click on create Button


Step 5 After some some time you will be able to see your created apps then click on it


Step 6  Then click on Required permissions under API Access and then click on Add


Step 7  Click on Select API and then Select Dynamics CRM Online and Click on Select


Step 8  Select Access CRM Online as organisation users checkbox and click on Select Button


Step 9 Then click on Done



Build the Console Application 

Step 1 Navigate to Microsoft Url for sample for web API or click here quick start sample and extract to your desired location


Step 2 Open the Solution file in vs and you have to edit the code by reading the comment with TODO tag


Step 3 Update the service URLwith your current CRM organisation

Eg:
        private static string serviceUrl = "https://sa##x.crm8.dynamics.com";   // CRM Online



To get the client ID go to your app in Azure and copy the GUID specified for Application ID


To get the client ID go to your app in Azure and copy the GUID specified for Application ID and redirecting URL to Home Page URL ( will be same as you gave at the time of app creation in Redirecting URL)

Your Code should look like below

private static string serviceUrl = "https://sablecafex.crm8.dynamics.com";   // CRM Online
private static string userAccount = null;  //CRM user account
private static string domain = null;  //CRM server domain
private static string clientId = "e5cf0024-a66a-4f16-85ce-99ba97a24bb2";
private static string redirectUrl = "http://localhost";  //e.g. "http://localhost/SdkSample"

Step 3 Update Authentication Context with direct Authorisation Endpoint URL

To get Authorisation Endpoint URL go to your app in Azure click on Endpoints button


And copy the OAUTH 2.0 Authorization Endpoint URL


Replace the previous lines for getting authority information and update with the below code

string authority = "https://login.microsoftonline.com/e5cf0024-a66a-4f16-85ce-99ba97a24bb2/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority, false);



Your Code should look like below

//AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(
//        new Uri(serviceUrl + "api/data/")).Result;
//AuthenticationContext authContext = new AuthenticationContext(ap.Authority, false);

string authority = "https://login.microsoftonline.com/e5cf0024-a66a-4f16-85ce-99ba97a24bb2/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority, false);

AuthenticationResult authResult = authContext.AcquireToken(serviceUrl, clientId, new Uri(redirectUrl), PromptBehavior.Always);

The above code is connection with Prompt dialogue, for connection without prompt see my another thread - without prompt

Step 4 Rebuild

 Include the below reference to your project(to global config file the line before where your client credentials is configured) and change the .Net framework to 4.6.1 and rebuild 

"ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12"

for More details on it refer to my another thread here 


Step 5 Rebuild Your code and run it you will be prompted with sign in page enter your credentials


Step 5 Press on Accept


 And Your are done


You can have a look at my code here : My Sample Code

Inspired By : Nishant Rana's Weblog

Happy Coding and CRMing 😊

SOURCE : mscrm.com