Stepping towards the enhancement, Dynamics CRM 2016 introduced ‘Web API’ as the new concept which delivers a unique development experience across a wide variety of programming languages, devices, and platforms.

We have earlier explored using WEB API through scripting from within Dynamics CRM environment in this blog.

We noticed quite a few queries coming up in forums about using WEB API through C# in code executed outside of CRM like a portal or other integration apps.

What is the difference?

The key difference lies in authentication. When we use scripts and execute from within Dynamics CRM using web resources, the authentication is automatically passed and we do not have to supply the authentication token header there.

But when we are connecting from outside of CRM context, it is important to authenticate the access credentials before they can connect and perform actions in Dynamics CRM.

Note: Some part of the code provided below has been taken from the Authentication library supplied in the SDK and that was the starting point for us to understand and get this working. We were however not able to get the code to execute as is and we try to explain the issues and resolutions to those through this blog.

Connecting using C# from a windows forms application

When using windows forms, we realized that it was important to initiate the code as an async task to establish connection. If you do not initiate an async task, the code block to authenticate fails since that action is async.

Suppose you want to create a record in CRM then we need to create an HttpRequestMessage which needs to be sent.

//Http Request needed to be sent by the HttpClient
HttpRequestMessage request = null;
try
{
request = new HttpRequestMessage(HttpMethod.Post, “api/data/v8.0/accounts”); //uri to accounts
request.Content = new StringContent(content.ToString()); //JObject of the data to be posted.
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(“application/json”);

CRM Online

In CRM Online, the OAuth authentication is performed through the Azure Active Directory. With OAuth the user is presented with the login page to type in the credentials manually, once the user is successfully authenticated, the control is redirected to the application that had invoked the authentication.

The following details are set in the config file for CRM Online connection

Steps for registering the App in Azure to get the ClientID:

For Online CRM deployment models, it seems to be a tricky process as we need to use OAuth to authenticate the user to Azure Active Directory and hence we need to register the App on Azure to get the ClientId & Redirect URL which will be used for Azure authentication.

Steps for registering the App in Azure:

If you have a trial online CRM then you also need to have an Azure subscription. You may get the trial account for Azure using the credentials for Office 365 by signing up from this link. Make sure to sign up in the same tenant.

After you have signed up for Azure go to the Azure portal i.e. https://portal.azure.com/ and navigate to ‘Browse’ and click ‘Active Directory’. You are then directed to the page as shown in the screenshot below.

From the left-hand side after selecting ‘ACTIVE DIRECTORY’, you are able to find the Active Directory which is created for the Organization. Click and open the active directory.

At the bottom click ‘ADD’ to add the application. A pop up would then get displayed on the screen.

Give a name for the App and select ‘NATIVE CLIENT APPLICATION’ and proceed to the next step.

You then need to specify the Redirect Url which can be used in the Code later as above.

Your App as seen in the screenshot below is now ready.

Next, click ‘CONFIGURE ACCESS TO WEB APIS IN OTHER APPLICATIONS’ followed by ‘Configure it Now’

You will then get directed to the screen as shown below:

Here you can see the Client ID and Redirect URL which needs to be used in the code further.

As you scroll down, you can view the web page as shown below.

After clicking ‘Add application’ you would be directed to a screen which displays the list of Microsoft Apps.

Select the Dynamics CRM Online option and add it. Next, the screenshot as below lets you provide the access to the user you wish.

Conclusion:

Hope the above code snippets along with the logic helps you to perform various operations through the Web API using C#. We have referred to the blog posted under Microsoft’s Developer Network for creating our WebApiHandler class.