Web API Enhancements in Dynamics CRM Update 1

By | May 27, 2016

Introduction:

Microsoft Dynamics CRM 2016 introduced a new concept called Web API which can be used across wide variety of devices, programming languages and platforms. With the release of Update 1 for CRM 2016, few enhancements were added in this. In this blog, explained about Enhancements of Web API which is more helpful for the Developers.

Mainly, there are two enhancements released for the Web API Queries.

1. Filtering records based on the Parent entity Attribute

In one of our earlier blog, we have explained how to read the records based Lookup field using prefix “underscore” and suffix “value”. But, now with Microsoft Dynamics CRM 2016 update 1 we can filter the records using Parent entity Attribute.

Suppose, if we want to retrieve the opportunities of a particular Account then we can use the Lookup field name and primary attribute of the Parent entity which helps to retrieve the Opportunities of that Account.

Refer below Query for above example:

Query:

https://[OrganizationURI]/api/data/v8.1/opportunities $select=name&$filter=parentaccountid/accountid%20eq%205A544BE0-6217-E611-80DE-C4346BDDC0A1

Description:

parentaccountid: It is the lookup field name on the Opportunity entity.

acountid: It is primary attribute of the Account entity.

Response:Web API Enhancements in Dynamics CRM

Also, you can refer the below code to run above query using Ajax request:

//this below code snippet retrieves opportunities of particular Account

function retrieveOpportunities() {
    try {

        //query
        var oDataUrl = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/opportunities?$select=name&$filter=parentaccountid/accountid%20eq%205A544BE0-6217-E611-80DE-C4346BDDC0A1";

        //ajax request to retrieve the opportunities of particular account
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUrl,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Accept", "application/json");
                xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                xhr.setRequestHeader("OData-MaxVersion", "4.0");
                xhr.setRequestHeader("OData-Version", "4.0");

                xhr.setRequestHeader("Prefer", "odata.include-annotations=*");

            },
            success: function (data, textStatus, xhr) {
                if (data != null) {

                    if (data.value[0].name != null) {
                        var opportnutyName = data.value[0].name;
                    }


                }
            },
            error: function (xhr, textStatus, errorThrown) {
                Xrm.Utility.alertDialog(xhr.statusText);
            }
        });

    } catch (e) {
        Xrm.Utility.alertDialog(e.message + "\n" + e.description);
    }
}

Note: We can only filter the records based on the Primary Attribute of the Parent entity.

2. Retrieve Parent Entity attributes by using expand command.

As same in our earlier blog, we have explained that we can read the parent entity attributes but in that there was a limitation that we can query for the single record and not for the collection of records. Now, with the CRM 2016 update 1 we can read the Parent entity attributes in the collection of records using the below query. Suppose, if we are retrieving the all Opportunities and also need to retrieve Account’s City then we can use the below query.

Refer below Query for above example:

Query:

https://[OrganizationURI]/api/data/v8.1/opportunities?$select=name&$expand=parentaccountid($select=address1_city)

Description:

                expand: It is command to retrieve the parent entity attributes.

                parentaccountid: It is lookup field name on the Opportunity entity.

                address1_city: It is field name of the parent entity which is to be retrieved.

Response:Web API enhancemnets

Conclusion:

Hope this two enhancement added in Web API Query will be helpful for developers.

We will cover more about Web API in our coming blogs.

Before you move on to the next post, have a look at Click2Clone – New Productivity add-on by Inogic.