Back to Basics # 50: Retrieve Data Using CRMWebAPI with Webresource in Dynamics CRM

0
37
Finally a CRM System Built Specifically for Independent Sales Reps


Pexels.com
” data-medium-file=”https://venkatasubbaraopolisetty.files.wordpress.com/2022/04/pexels-photo-3861943.jpeg?w=300″ data-large-file=”https://venkatasubbaraopolisetty.files.wordpress.com/2022/04/pexels-photo-3861943.jpeg?w=640″ src=”https://venkatasubbaraopolisetty.files.wordpress.com/2022/04/pexels-photo-3861943.jpeg?w=640″ alt=” ” class=”wp-image-3502″ width=”839″ height=”558″ />

Introduction:

In Dynamics 365 CRM, for certain requirements data needs to be retrieved to achieve business functionality with Web API.  As an example, selected contact record guid was passed to fetch details of contact .

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, in JavaScript file(webresource) use the below code to fetch the contact GUID for a selected record

let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

as shown in the below figure

Step 4:

After Step 3, prepare url with the combination of url of Dynamics crm environment and frame web api path by passing dynamic contactGUID with the below code

var finalurl = encodeURI(Xrm.Page.context.getClientUrl() + “/api/data/v9.2/contacts(“+contactGUID+”)”);

As shown in the below figure

Step 5:

After Step 4,  pass this final url to the request and frame the request by passing all the required headers and if we get response with 200 status code then show Full name of the contact with the below code

var data = null;

var isAsync = false;

var req = null;

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

}

else if (window.ActiveXObject) {

req = new ActiveXObject(“MSXML2.XMLHTTP.3.0”);

}

req.open(“GET”,finalurl,isAsync);

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=\”*\””);

req.onreadystatechange = function () {

if (this.readyState === 4) {

req.onreadystatechange = null;

if (this.status === 200)

{

 data = JSON.parse(this.response);

  if(data!=null)

      {

      Xrm.Utility.alertDialog(“Full Name:”+data.fullname);

      }

}

else

  {

  Xrm.Utility.alertDialog(this.statusText);

  }

}

};

req.send();

As shown in the figure

Step 6:

After Step 5,  final code looks like below

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        getContacts(executionContext);

        },

    __namespace: true

}

function  getContacts(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

            let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

var finalurl = encodeURI(Xrm.Page.context.getClientUrl() + “/api/data/v9.2/contacts(“+contactGUID+”)”);

var data = null;

var isAsync = false;

var req = null;

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

}

else if (window.ActiveXObject) {

req = new ActiveXObject(“MSXML2.XMLHTTP.3.0”);

}

req.open(“GET”,finalurl,isAsync);

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=\”*\””);

req.onreadystatechange = function () {

if (this.readyState === 4) {

req.onreadystatechange = null;

if (this.status === 200)

{

 data = JSON.parse(this.response);

  if(data!=null)

      {

      Xrm.Utility.alertDialog(“Full Name:”+data.fullname);

      }

}

else

  {

  Xrm.Utility.alertDialog(this.statusText);

  }

}

};

req.send();

}

}

And register this function on load event in contact form and save and publish it.

Step 7:

After Step 6,  open any contact record and observe full name is shown as an dialog.

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure the final url was well formed otherwise lead into errors.
  3. As my concentration is on explaining way to fetch data with web api  I have not explained way to register onload event in contact form.

Conclusion: In this way, one can easily fetch contacts data for a selected contact record using  CRM Web API.



Source link

Leave a reply

Please enter your comment!
Please enter your name here