Dynamics 365 Web API enhancement Part 1

Dynamics 365 released some new enhancements to Web API. If you are new to Web API, we will suggest you to refer our earlier articles for Web API. In this release create and update Web API requests are enhanced to return entity object after record created or updated. Let’s understand this enhancement using following create request example.

Let say we want to create email activity record using Web API. Following is the example of using create Web API request used before Dynamics 365.

function createEmail() {
    var serverURL = Xrm.Page.context.getClientUrl();
    var email = {};
    email["subject"] = "Email demo from Web API";
    email["description"] = "This a web api test";
    email["regardingobjectid_contact@odata.bind"] = "/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)";

    //activityparty collection
    var activityparties = [];
    //from party
    var from = {};
    from["partyid_systemuser@odata.bind"] = "/systemusers(8D23B2C1-9869-4C3F-9A80-BA51375C1784)";
    from["participationtypemask"] = 1;

    //to party
    var to = {};
    to["partyid_contact@odata.bind"] = "/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)";
    to["participationtypemask"] = 2;

    activityparties.push(to);
    activityparties.push(from);

    //set to and from to email
    email["email_activity_parties"] = activityparties;

    var req = new XMLHttpRequest();
    req.open("POST", serverURL + "/api/data/v8.0/emails", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "return=representation");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 201) {
                var emailUri = this.getResponseHeader("OData-EntityId");
                }
            else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send(JSON.stringify(email));
}

When we will execute this request, we can see screen shot of watch window, we are not getting any repose, it’s blank. Older request only returns responseheader which contains URI of the new record created. You can see also see it’s returning status as 204.

apienhancement2

Whereas using new Web API enhancement, we can now include additional preference (return=representation) in the header of the request, to get entity object which is just created like following:

req.setRequestHeader("Prefer”,”return=representation");

So after adding this addition header request, we will get response like following and it will also return status as 201 instead of 204.
emaiapi1

We can get entity object from response and process fields accordingly if required.

Stay tuned for more Dynamics 365 new features !!

5 thoughts on “Dynamics 365 Web API enhancement Part 1

  1. Pingback: Dynamics 365 Web API enhancement Part 1 - Microsoft Dynamics CRM Community

  2. Pingback: Dynamics 365 Web API enhancement Part 2 | HIMBAP

  3. Kevin

    This is a great code snippet. How can I use a similar code to accomplish my goal of adding portal comments using the web api?

    Reply

Leave a Reply to HIMBAP Cancel reply

Your email address will not be published. Required fields are marked *