16/10/2012

How to Use (REST) OData endpoint in CRM 2011


Simple Sample - How to use "ODATA" endpoint in CRM

Overview

There are two ways to write a client script to manipulate CRM data by code:
  • SOAP endpoint is recommended for retrieving metadata, assigning records and executing messages programmatically.
  • REST endpoint is recommended for creating, retrieving, updating and deleting, associating and disassociating the records.

 In this scenario, I will use REST (aka Organization data service - OData) to extract records out of CRM.

Simple Sample:

On load event of the Account form we want to show a message with the first name and country of primary contact of the selected account.
We are going to write a Java script which will be executed on load even of the Account form and we will use OData to retrieve the primary contact’s full name and Country

Solution

First we need to create a webreource and hook it to the “onLoad” event of the account, and then we need to write a code and use organization data service to access the primary contact of the account. For keeping everything simple, we will assume the primary contact is mandatory for an account on the time of creation (we won’t check if the primary contact is null)

Here are the actual steps (I assume you are at least at intermediate level in CRM and how to define webresources and add them to your form)

Add REST library and JSON from SDK

We are going to add JSON and SDK REST Javascript generic libraries from SDK to our CRM. These libraries are defined inside the SDK sample files and using them makes it very easy to write a readable code.
Let’s add those libraries. Create two new webresources and call them SDK_JQUERY and JSON.
For each library add the equivalent library from your SDK folder
“YOU SDK FOLDER \sdk\samplecode\js\restendpoint\javascriptrestdataoperations\javascriptrestdataoperations\scripts” and add sdk.rest.js
We need sdk.jquery and json2.js
Add REST SDK CRM 2011.jpg
Add Sdk Json.jpg

Create a webresource for our account form in CRM





Add the web resources we have created to account form libraries

Open the account form (the form below is not out of the box and is modified)


Click on form properties



Click on add under “Form libraries” and add the web resource we have created earlier to the form


Click on add under “Form libraries” again to add the two web resources from SDK library


In the "even handlers" section, click on add and bind the onLoad event handler


Double click on the form library you have just added and start adding your javascript:
function onLoad()
{
    
    alert("You are in the LoadContact function ");
    var primaryContactId =  Xrm.Page.data.entity.attributes.get('primarycontactid').getValue()[0];
    retrieveContact(primaryContactId.id);

}


function retrieveContact(contactId)
{
    SDK.REST.retrieveRecord(
     contactId,
     "Contact",
     null, null,
     function (contact)
     {
         alert("Retrieved the primary contact named \"" + contact.FullName + "\". This account was created on : \"" + contact.CreatedOn + "\".");
     },

     function ()
     {
         alert("Error occured.");
     }
   );
}


Using the SDK makes it really easy to use libraries to retrieve information or create and update field. Here is a useful like on how to use other methods in the library: http://technet.microsoft.com/en-us/library/gg309549.aspx#BKMK_CrmODataOperationsHTML

2 comments:

  1. I got Access Denied error when i used this code.Can you suggest what might be the issue?

    ReplyDelete