OData endpoint with web resources

od
In today’s post i want to talk about how you can use the OData endpoint to execute HTTP requests using URI based service to work with Microsoft Dynamics CRM.
Note that though the OData endpoint provides a varied operations it is not fully implemented.
If you are not familiar with the OData protocol please check the following post
Within the application the authentication is already provided but if you use the endpoint outside the application, you must .
A quick reminder on What is OData.
OData endpoint uses the Open Data protocol which implements a RESTful design pattern.
REST represents Representational State Transfer,it is an architectural style in which every resource is addressed by using a unique URI which in Microsoft Dynamics CRM can be an entity collection or a record.
REST works by using HTTP methods such as GET, POST, MERGE, and DELETE and hence, provides a standard interface that you can use with any programming language. RSET allows to process operations both synchronous or asynchronous.
OData sends and receives data using ATOM (an XML-based format usually used for RSS feeds) or by using JSON.

The Organization Data Service endpoint in Microsoft Dynamics CRM is a WCF Data Services framework which provides a REST-based data service.
It can be accessed using the URI found in the Developer Resources under the Organization Data Service.
Only CRUD actions can be performed on entity records ,messages that require the Execute message can’t be performed.

Here is a few examples (using the SDK.REST.js library that can be found in the SDK) of how the ODATA endpoints allows us to perform CRUD actions in Dynamics CRM.

function CreateAccount() {
// JScript source code 
 var account = {};
 account.Name = "Contoso Ltd";
 account.Revenue = { Value: "500000000.00" }; //Set Annual Revenue
 account.EMailAddress1 = "ceo@contoso.com"

 //Create the Account
//We used the SDK.REST.js library but many more are available such as SDK.jquery.js  
SDK.REST.createRecord(
     account,
     "Account",
    function (Account) {
        //OnSuccess Callback
    },
    function (error) {
        //OnError Callback
    });
}

function RetrieveAccount(AccountId) {
 SDK.REST.retrieveRecord(
     AccountId,
     "Account",
     null, null,
     function (Account) {
        //OnSuccess Callback
    },
    function (error) {
        //OnError Callback
    });
   );
}

//retrieve top 5 accounts( only get their accountid and name)
function RetrieveAccounts() {
 SDK.REST.retrieveMultipleRecords(
    'Account',
    'var options = "$select=Name,AccountId&$top=5",
    function (values) {
        //OnSuccess callback
      if (values[0] != null) {
       firstAccount = values[0];
      }
      else {
       writeMessage("No accounts");
      }
    },
    function (error) {
        //OnError callback
    },
    function () {
        //Fires once all data pages have been retrieved.
    });
}

AS you can see we can refine the query results by using system query options.
The following lists the query string options defined in the OData protocol that are implemented in the OData endpoint for Microsoft Dynamics CRM (updated to 2015).
$select – Specifies a subset of properties to return.
$expand – allows you to include related entities records in the main entity records.
You should locate the name of the entity relationship that defines this relationship.
for instance, retrieve accounts related opportunities
/AccountSet?$select=Name,opportunity_customer_accounts&$expand=opportunity_customer_accounts
By default, you can define up to six relationships to expand
$filter – Specifies an expression or function that must evaluate to true for a record to be returned in the collection.
used with the logical operators: eq (equal),ne (not equal),gt (greater than),ge (greater than or equal),lt (Lower than),le (lower than or equal),and,or,not
Equal
/AccountSet?$filter=Address1_City ne ‘New york’ and CreditLimit/Value ge 1000
$orderby – Set the values that used to order a collection.
$skip – Sets the number of records to skip before it retrieves records in a collection.
$top – sets the maximum number of records to return.

function UpdateAccount(AccountId) {
 var account = {};
 account.Name = "Constoso Jr.";
 account.Address1_City = "New York";
 account.Address1_PostalCode = "405634";
 account.Address1_StateOrProvince = "NY";
 account.EMailAddress1 = "cto@contoso.com"

 SDK.REST.updateRecord(
     AccountId,
     account,
     "Account",
      function (Account) {
        //OnSuccess Callback
    },
    function (error) {
        //OnError Callback
    });
}

function DeleteAccount(AccountId) {
 SDK.REST.deleteRecord(
       AccountId,
       "Account",
   function (Account) {
        //OnSuccess Callback
    },
    function (error) {
        //OnError Callback
    });
 }



function associateAccounts(parentAccount, childAccount) {
 /// Associates two account
 SDK.REST.associateRecords(parentAccount.AccountId,
	"Account",
	"Referencedaccount_parent_account",
	childAccount.AccountId,
	"Account",
     function () {
        //OnSuccess Callback
    },
    function (error) {
        //OnError Callback
    });
}

Published by Yaniv Shoval

.Net developer who takes interest in various technologies but specialized in .NET and Microsoft Dynamics CRM development

Leave a comment