Wednesday, 7 September 2016

Explore Web API in Dynamics CRM 2016

Microsoft has just published Dynamics CRM Web API first sample set. Read more about this here.

One important platform update that came in with Dynamics CRM 2016 was the introduction of WEB API or ODATA v4, the vNext of ODATA protocol that had been in use until CRM 2015. The advisory then was a deliberate move to using WEB API moving forward and the deprecation of the ODATA API.

Early June came in the Deprecation announcement with the release of Dynamics CRM Online Update 1.

As one of the early adopters of this technology early this year we started exploring this. At that time we did not find an API library similar to the SDK.REST.js being shipped with the SDK and we still did not have an equivalent of the popularXRMServiceToolkit.

This required us to go down the path of writing down the code snippets ourselves for the common CRUD operations as well as the enhanced operations that WEB API is now capable of over the traditional ODATA v2 endpoint that we were aware of until now.

With the deprecation announcement it felt the time was just right to come out with a post to encourage developer to review and make the switch to WEB API soon. In this post, I will list out quick links to code snippets that allow of easier understanding of the new service endpoint.

Querying Data: While querying data with new endpoint remains the same, there are a few changes when it comes to filtering data by lookup fields. One important change to note is that the entity collection is not referred to as <entityname>Set any more. Instead they are referred to by the Plurals so “Accounts collection” would be “accounts”. You can confirm the name by navigating tohttps://org.datacenter.dynamics.com/api/data/v8.0/.

Do watch out how the results are to be parsed and how to request additional details in the query results by passing appropriate header tags in the requests. 
Execute FetchXML using WEBAPI: For developers who are comfortable building queries using FetchXML and would like to continue querying data using FetchXML instead of the ODATA/WEBAPI queries. This article should help you get going with that.

CRUD Operations: The next article here is to help with the basic CRUD operations. Once again we need to pay attention to setting the values of lookup. Also the Guid for a create operation is now returned in the response header instead of the responseHTML.

Check this article on setting the values for DateOnly attributes. In case of DateOnly, just pass the “Date” not the time component.
If you are working on an external integration application developed using C#, look herefor supporting code.

Impersonation: Impersonation refers to performing an operation under the context of a user other than the logged in user. All operations performed through scripting are executed under the context of the logged-in user.
However at times you want to perform some behind the scene operation under elevated permissions. This can now be achieved by passing the userid of the super user in the request header.

Using WEBAPI for custom integrations through C#: Most examples that we found were around using WEB API with scripting from within CRM through webresources. The key point here is to register you application with Azure Active Directory. The code needs to acquire the security token to be able to authenticate the request and perform the operation.
Execute Web API Request using Angular JS in Dynamics CRM: And yes with the trend of now developing CRM web resources using the popular Angular JS framework. This was our attempt at implementing WEB API within an Angular JS. Use $http to make the WEBAPI call :-)

Conclusion:
I am sure there would be better implementations that would be possible and hopefully there would be popular libraries out that developers can reuse. But until then, the articles here should be good enough to get you started.
One of the important limitation with WEBAPI to consider when making the choice between WEB API or SOAP is that WEB API is not supported in Offline Outlook Client. Any code written using WEBAPI calls will not work if the user is accessing these functions through an Outlook client when in offline mode. If you are designing solutions that require access in offline mode to, you might want to wait until it is available in offline mode too. I am hoping this is supported soon in the upcoming releases.

P.S. As of the day of posting, Microsoft SDK team has released an exhaustive set of code samples that might be better implementations of the examples explained in the list above. You are also encouraged to go ahead and a project on github that would help designing js code for CRM WEB API operations.    

No comments:

Post a Comment