Not able to understand the setRequestHeaders of XMLHttpRequest in CRM WebApi

5 views
Skip to first unread message

vijay kumar

unread,
Jul 18, 2019, 5:37:50 AM7/18/19
to Membership Management System Software on Microsoft CRM
Hi All,

I had a 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 on change 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 the 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 a 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 every time 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? 

Vikranth Sinha

unread,
Aug 13, 2019, 5:55:54 AM8/13/19
to Membership Management System Software on Microsoft CRM
Hi,

These first four headers from the below lines of script are really needed when the web API 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 option sets 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=\"*\""

Reply all
Reply to author
Forward
0 new messages