Saturday, September 8, 2012

Fetch/Retrieve Entity Using Query Expression

Problem :
               Need to fetch/retrieve the entity record using the query expression.
               I have spend lot of time in fetching the different entities record and now I got some time to share this learning with all of you so that if anyone of you who is beginner and want to learn the fetch/retrieve of the entity record then he/she will learn something from here. I know there are lot of post available on internet but I want to share what I have learn with easy words so that you people can understand it easily.
I have used the Silver light web resource for this mean to fetch/retrieve the entity record and I have used the SOAP endpoint to fetch/retrieve the entity record. For you learning if you don’t have idea how to used the fetch end points in your web resource like Silver light then you can read the following article by the CRM 2011 SDK. Here you will learn what initial steps are need to add the SOAP endpoint and it will also give some introduction to the two classes which SOAP endpoint will use and what changes you need to do in the Reference.cs file.
Let us start the example code which is used to fetch the entity records. Here I have used the account entity to display data without any condition, all accounts record are fetched and displayed in the grid and you can see the output in the Image 1 as shown below.

Image 1
In Image 1 you can see that I have used the data grid control of the Silver light to show the account entity record which contains the Account Name, Main Phone, Primary Contact, Email and Account number attributes of the accounts.I have used the MVVM approach to bind the data grid control of the Silver light, so all of my code will be in the view model of which is named MainViewModel and is placed in the ViewModel folder.
Code to fetch the account entity record is shown in List 1; here you can see that I have used the queryExpression class to retrieve the account entity.Here you can see that after declaring and initialization of the query expression object I have set the name of the entity in the second statement ( I have used the account entity and you can changed it whatever your entity name like contact, lead etc).Next is to set the which attributes need to be fetched from the account entity, for this you can used the Column set property of the query expression and give an array of string which contain the schema name of the attributes . Here you can see that I have used the accountid, name, statecode, statuscode, primarycontactid, accountnumber, emailaddress1, telephone1, address1_shippingmethodcode attributes name to be fetched.
If you want that your return records are in specific order by some attribute name then you can use the order property of the query expression and give the attribute name by which you want your record to be ordered. Here I have used the name attribute of the account so that my records are order by account name Ascending

List 1

After setting all the required properties of the query expression next is to create the Organization service object to send the query expression and retrieve the entity records. Here you can see in the constructor of the Organization service I have passed the request name “RetrieveMultiple“ as I am retrieving not single records as I didn’t add any condition in the query expression to get only specific record.For complete list of request name you can check the following link.Next is to create the IOrganizationService object and then pass the request and the call back function name and the addition data which you need on the call back function.
In the call back function first statement is to end the request which was send and store the return object in the OrganizationResponse which is return by the EndExcute function(as we used BeginExecute of the IOrganizationService so we need to call the end of function which we are calling if you call BeginRetrieveMultiple then you need to call EndRetrieveMultiple in the call back function).Then by using the Organization response object need to get the EntityCollection and store it in the EntityCollection object for further manipulation as we need to display the attribute value in the data grid. After storing the EntityCollection in EntityCollection object next is to check the count of the records if no record then display message else use the foreach loop to create the collection which is bind to the data grid control to display account entity records. Here you can see that I have used the GetAttributeValue of the extension method to get the value of the specific attribute.

Note: The data grid style used in this example is taken from following link

You can download the source code of the sample from here.
and working CRM 2011 solution from here.

Go to Home

All and any comments / bugs / suggestions are welcomed!

No comments: