Friday, June 21, 2013

Using OData endpoint in MS CRM 2011

As most of you already know in CRM 2011 there are two ways which we can write client script to work with CRM Data.

  • SOAP endpoint: recommended for retrieving metadata, assigning records, executing messages 
  • REST endpoint: recommended for create, retrieve, delete and update, associating and disassociating records, 

Here I’m explaining one of the easiest ways to retrieve data using OData query designer.

Download Link OData Query designer: http://crm2011odatatool.codeplex.com/
For this example I’m getting Template Id by Name,
First Generate query using CRM 2011 OData Query Designer

 

function GetTemplateId() {

    var query = serverUrl + "xrmservices/2011/OrganizationData.svc/new_interventiontemplateSet?$select=new_interventiontemplateId&$filter=new_WebResourceName eq '" + webName + "'";
    var templateId;

    jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: query,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.            
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                templateId = data.d;
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the Template");
        }
    });

    return templateId;
}
Update Record:
function Update(id, entityObject, odataSetName) {
        var jsonEntity = window.JSON.stringify(entityObject);
        var serverUrl = Xrm.Page.context.getServerUrl();

        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
        datatype: "json",
        data: jsonEntity,
        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        },
        success: function (data, textStatus, XmlHttpRequest) {
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {

            if (XmlHttpRequest && XmlHttpRequest.responseText) {

                alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
            }
        }
    });
}
You can call this function like below;
     var pp = new Object();
     // Update fields
     pp.new_ApprovedOn = "2013/12/06";
     Update(accountId, pp, "accountSet");
MSDN Sample for JSON : http://msdn.microsoft.com/en-us/library/1bb82714-1bd6-4ea4-8faf-93bf29cabaad#BKMK_UsingJQuery

OData System Query Options Using the REST Endpoint : http://msdn.microsoft.com/en-us/library/gg309461.aspx

No comments:

Post a Comment

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...