web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 314

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

I have the same question (0)
  • Verified answer
    Palani Profile Picture
    196 on at

    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.

  • Suggested answer
    Ishan Sharma Profile Picture
    314 on at

    Thanks to make me understand!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans