Saturday, April 9, 2016

With new Web API, we can execute System Views, Personal Views and  Custom fetch xml using the Web API.In this blog i am going to show how to execute custom fetch xml using the Web API

In my example i am going to get All  Active Accounts from CRM by using Web API which will use Fetch XML

1.Download the fetch xml query from CRM

2.Replace double quote(") with single quote('),now the query should be like below

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='account'>
    <attribute name='name' />
    <attribute name='accountid' />
    <order attribute='name' descending='false' />
    <filter type='and'>
      <condition attribute='statecode' operator='eq' value='0' />
    </filter>
  </entity>
</fetch>

3.Inorder to Test the Encoded URI  Fetch XML query result open the developer tools in the chrome browser and pass the fetch xml query to encodeURI method




4.You should get the Output as excepted
{
  "@odata.context":"https://test.crm5.dynamics.com/api/data/v8.0/$metadata#accounts(name,accountid)","value":[
    {
      "@odata.etag":"W/\"655876\"","name":"A. Datum Corporation (sample)","accountid":"9f607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639125\"","name":"Adventure Works (sample)","accountid":"93607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639127\"","name":"Alpine Ski House (sample)","accountid":"9d607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639129\"","name":"Blue Yonder Airlines (sample)","accountid":"97607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639131\"","name":"City Power & Light (sample)","accountid":"99607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639133\"","name":"Coho Winery (sample)","accountid":"a1607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"552842\"","name":"Contoso Pharmaceuticals (sample)","accountid":"9b607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639135\"","name":"Fabrikam, Inc. (sample)","accountid":"95607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639137\"","name":"Fourth Coffee (sample)","accountid":"8f607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"639139\"","name":"Litware, Inc. (sample)","accountid":"91607087-2eed-e511-80ef-3863bb2e9c50"
    },{
      "@odata.etag":"W/\"632178\"","name":"Tailspin Toys","accountid":"83a1bf12-73f3-e511-80eb-3863bb347ef0"
    },{
      "@odata.etag":"W/\"655763\"","name":"test","accountid":"562eac95-4cfe-e511-80f1-3863bb2e4de0"
    }
  ]
}

5.Now  make the GET request using the FetchXML shown below,use encodeURI to Encode the Fetch XML Query
function OnLoadAccount()
 {
  
     var fetch = encodeURI("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='account'><attribute name='name' /> <order attribute='name' descending='false' /><filter type='and'><condition attribute='statecode' operator='eq' value='0' /></filter></entity></fetch>");
     var entityname = "accounts";
     var serverURL = Xrm.Page.context.getClientUrl();
     var Query = entityname + "?fetchXml=" + fetch;
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.0/" + Query, false);
    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.onreadystatechange = function ()
     {
         if (this.readyState == 4 /* complete */) 
        {
            req.onreadystatechange = null;
            if (this.status == 200)
            {
                var data = JSON.parse(this.response);
                if (data != null)
                 {
                    
                }

            }
             else
             {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send();

}

Happy CRM Learning !!!!!!!!!!!!
Thanks
Ramakant



No comments:

Post a Comment