Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Not able to understand the the setRequestHeaders of XMLHttpRequest in CRM WebApi

Posted on by 308

Hi All,

I hada very simple requirement which is my contact details like Name,Email,Phone,Jobtitle should be populated on lead form as soon as i select the contact from parentcontactid lookup in lead form so it is clear that the code which i need to write will be bound to onchange event of the Lookup control. below is my code.

function populateDetails(executionContext)
{
var context = executionContext.getFormContext();
if (context.getAttribute("parentcontactid").getValue() != null)
{
var contactid = context.getAttribute("parentcontactid").getValue()[0].id.slice(1, -1);


var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + contactid + ")?$select=emailaddress1,firstname,jobtitle,lastname,mobilephone,telephone1", true);

req.send();
req.onreadystatechange = getDetails;

}/*End of parentcontactid Check*/
}

function getDetails()
{
if (this.readyState == 4)
{
this.onreadystatechange = null;
if (this.status == 200)
{
var result = JSON.parse(this.response);
Xrm.Page.getAttribute("firstname").setValue(result["firstname"]);
Xrm.Page.getAttribute("lastname").setValue(result["lastname"]);
Xrm.Page.getAttribute("fullname").setValue(result["firstname"]+" "+result["lastname"]);
Xrm.Page.getAttribute("jobtitle").setValue(result["jobtitle"]);
Xrm.Page.getAttribute("emailaddress1").setValue(result["emailaddress1"]);
Xrm.Page.getAttribute("telephone1").setValue(result["telephone1"]);
Xrm.Page.getAttribute("mobilephone").setValue(result["mobilephone"]);
}
}
}

The above code is perfectly running fine but i have also seen below code in  many places, i just don't understand if above code is running perfectly then why do we need below code as well? i just need to know what all cases below code will be used since it is difficult to  remember. we can also see this code when we generate request from REST builder

req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

we all know whenever the readyState changes the onreadystatechange even triggers so onreadystatechange event triggers everytime the readyState change. So if readyState is 4 which means request complete and response is ready similarly status 200 means okay but why this requestHeaders are being used in CRM WebApi? and what will happen if i don't use these headers?

Thanks in Advance!

Ishan  Sharma 

*This post is locked for comments

  • Suggested answer
    Ishan Sharma Profile Picture
    Ishan Sharma 308 on at
    RE: Not able to understand the the setRequestHeaders of XMLHttpRequest in CRM WebApi

    Thanks to make me understand!

  • Verified answer
    Palani Profile Picture
    Palani 196 on at
    RE: Not able to understand the the setRequestHeaders of XMLHttpRequest in CRM WebApi

    Hi Ishan,

    These first four headers from the below lines of script are really needed when the webapi web service that we are trying to consume supports different versions of odata protocol and supports different formats of request and response.  In case of dynamics CRM, the Web API service is designed to accept and respond to requests in JSON format by default and the odata version used for building the web API service is 4.0 and thus it works for you without those headers.

    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    The last line of the script is really optional and can be used based on the requirement whether you need formatted values of the optionsets which by default is not sent as part of the response unless you include this header.  There are other uses and formats of this header which you can read about from the links below.

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/query-data-web-api

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/web-api-types-operations

    In summary, it's always good to read basic details about the webapi service that we are trying to consume and send appropriate headers required by the service so that we receive the response in the expected format.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans