Prior to CRM 2016, REST queries were based on the OrganizationData service. However the functionalities provided by the REST API were really limited to the CRUD operations and Associate Requests. If you need to execute complex fetchxml queries, either you need to take your code to the server side or call the Organization service endpoint to execute your fetch xml. The problem with using the SOAP endpoint is that you need to construct the raw SOAP request and parse the response back.
I always wondered if we could have something in CRM with the power of SOAP endpoint and the simplicity of using url based queries like the REST endpoint. What a great combination it would make? Well I guess Microsoft has heard me and like me, many CRM consultants and have introduced the new Web API in Dynamics CRM 2016.
With Web API, you can execute System Views, Personal Views and even custom fetch xml using the Web API. Excited? So let’s explore it one by one. I will not be using any kind of code here. I will just demo this using the browser. After all, all the Web API queries are URL based. Isn’t it great!
Executing a System View using Web API.
Say I want to execute the ‘Active Contacts’ system view of the contact entity. For that first I need to get the viewid for the ‘Active Contacts’ view. So let’s get the id first. Below is the query for the same.
https://xrmtr11.crm5.dynamics.com/api/data/v8.0/savedqueries?$select=name,savedqueryid&$filter=name eq ‘Active Contacts’
The greyed out portion is your CRM Organization URL. Just take this URL and put in your browser. You would results as shown in the screenshot below.
This is JSON response. You can easily parse the response and get the id of the ‘Active Contacts’ view.
Now once you have the guid of the view, you can execute the view to get the results of the view with the below URL.
You would get the JSON result as shown above. As you can identify, you can see the contacts for your organization here.
Using the same approach, you can execute your personal views to get the results.
Execute custom fetchxml queries using Web API.
Below is the fetchxml to retrieve active accounts in the system.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<attribute name="name" /> <filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<link-entity name="contact" from="contactid" to="primarycontactid" visible="false" link-type="outer" alias="accountprimarycontactidcontactcontactid">
<attribute name="emailaddress1" />
Now I run the below query and it fetches me all the Active Accounts.
Below is the result for the same.
Hope this helps! Happy exploring CRM 2016.