Retrieve Multiple using Web API

Welcome to my old Blogspot blog! You can read this post on our new website: Read this post on our new website 

Following is a detail sample code to retrieve a record using Web API in Dynamics CRM.
/*
entityName: entityName is the plural entity logical name of entity e.g for account it is accounts. for opportunity it is opportunities
query: the odata query
return:-Entity records       
*/
function retrieveMultiple(entityName , query) {
    var data = null;
    var req = new XMLHttpRequest();
    req.open('GET', Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + entityPlurarName + 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.send();
    if (req.readyState == 4) {
        if (req.status == 200) {
            data = JSON.parse(req.response);
        }
        else {
            var error = JSON.parse(req.response).error;
            console.log(error.message);
        }
    }
    return data
}
You can call the above retrieveMultiple method as:
var data = retrieveMultiple('roles', "?$select=roleid&$filter=name eq 'Project Manager'");
    if (data != null && data.value.length > 0)
    {
        var PMRoleID= data.value[0].roleid;
    }

No comments:

Post a Comment