Thursday, May 5, 2011

Using JSON to retreive Data IN Microsoft CRM 2011

Ajax is always one of my favorite area of working for fetching and saving data in the database.It allows me to connect to server by staying on the client machine and the send request and after successfully return data from the server end working on the data to display on the user screen. In this post I will show you how to get the all the user using JSON in Microsoft CRM 2011. Let us start with our javascript code to fetch the information of the system user. JavaScript code for to fetch the system user information is listed in List 1. Here you can see that I have declare serverURL first in the beginning of the GetUserInformation function and assign it the value by concatenating the value of the getServerUrl which return the base server URL and then the end point of the service which in this case is the OrganizationData.svc. At the end of the serverURL you can see the name of the entity which is used to fetch the record here I have used the SystemUserSet. Each Microsoft Dynamics CRM entity is represented in the conceptual schema definition language (CSDL) as a collection using the element. The name of each collection follows the naming convention of [Entity Schema Name]+ Set. This name is used in the URL to access a collection of entity records


List 1
If you want to do operation such as retrieve , delete, insert and update on the account entity then you use the name AccountSet and same for the other entities as well. After setting the serverURL in the next statement I have created XMLHttpRequest object with the name getSystemUser and then set the properties of the getSystemUser object.
In the open function of the getSystemUser object I have passed the type which is either GET or POST as I am retrieving data so my request will be GET and the URL of the server which I have store in the serverURL which is either a relative or absolute URL may be used here. If an absolute URL is being used, most browsers implement a same-origin policy, meaning the URL being requested must be on the same domain as the domain from which the URL is being requested. and last parameter is for how request should be issued: True for asynchronous; False for synchronous.
In the next statement I have set the Header of the getSystemuser object and set the return type to json. and then set the onreadystatechange event for the asynchronous operation and then send the request by passing the null object.

Image 1

In the retrieveSystemUserCallBack function first is to check the readystate of the request which is the 4 for complete and then check for the status of the request which is 200 for successfully return of the request. Then in the next statement JSON.parse will be use which is used to Deserializes JavaScript Object Notation (JSON) text to produce a JavaScript value. JSON.parse is defined in Json2 which you can download from here. I have added the Json2.js file as web resource in the Microsoft CRM 2011. For those who don't have idea of how to add web resource and use them in the Microsoft CRM 2011 see the post by Ayaz Ahmed.  As we have specify that return value will be Json the results will be in the d property of the data object returned. When you are creating a record or retrieving a single record using the unique identifier, d will represent the data for the record. In every other case d will be an array. As you can see in the List 1 that after the Json.parse I have used the d property to get the value which is store in the retrievedUsers object.
At the end I have loop throught the retrievedUser collection and shown the full name of the user.The output of the request can be seen in the Image 1 which is displayed using the Json Viewer which is used to show the Json data in tree format. Hope you get idea of how to use the Json in OData to retrieved data using Microsoft CRM 2011.

All and any comments / bugs / suggestions are welcomed !

1 comment:

Dhillon said...

Nice Article!!! Thanks,