Monday, December 31, 2012

Reading Organization Setting

Problem:
               Need to read organization settings

Solution:
               To read the organization information like what is the maximum size of the file which can be uploaded? , Currency format code and currency symbol (like ($) dollar sign for USA etc), date and time format string for organization level, and fiscal year related information etc, we need to read the organization entity to get these and other organization related attributes.

Image 1

Code for the reading organization is listed in List 1, here you can see that it is simple query expression which read all the columns and no user based condition which I have added while reading the User setting (Where I have used the current user id to get the user settings).
List 1

After the call is send and it return control is shifted to "getEntityRecord_ClickCallback" call back to process the return value from the request. Code is listed in List 2, here you can see that I have used to for each loop on the list which contains the meta data of the organization setting entity. Here I fist check for if the entity contains attribute or not and then check the attribute type whether attribute is pick list or lookup type because to get value of the lookup and pick list is different.If you get the value of the pick list then it will give you integer part of the pick list and we need the string value of the pick list to be displayed. Here you can see that I have get the integer value of the pick list in local variable and then search for the string value of the pick list in the list ( I have also save pick list option set in local list of the record so that I don’t need to make another MS CRM call to get option sets of the particular pick list, you can also see the code of the call back of the meta data request of the organization setting entity for reference).
List 2
This was all how to get the organization setting to get organization setting. You can download source code and CRM 2011 solution for testing and modification from the links which are given below.
Downloads
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!

Sunday, December 30, 2012

Reading User Settings by User Id

Problem:
                Need to read user setting so that we can use some of the user setting like time zone to display correct time for the log-in user if our portal is use MS CRM 2011 user name and password to log-in.

Solution:
                To get login user setting like time format, date format, date separator, time separator, number separator and time zone code etc we need to read the "usersettings" entity. You can see the main screen of the sample application in image 1 here you can see that I have displayed all attributes of the user setting entity in data grid control. Here first I have retrieved the meta data of the user settings entity and then based on the current login user id I have retrieved the settings of the user from the MS CRM.

Image 1

Let me discuss code in the list List 1, here you can see that I have function “getEntityRecord“ which takes entity name as parameter and is called when the meta data request for the entity is completed and attributes are added in the collection.Here you can see that I have assigned passed entity name to the query expression object and then I just created on condition to set the system user id Guid. Here you can see that I have JavaScript to get the current login user id and then add to values of the condition. And then add that condition to the query expression object and set all columns of the query expression object to true as I want to get all the columns of the user setting entity

List 1

After the call is send and it return control is shifted to "getEntityRecord_ClickCallback" call back to process the return value from the request. Code is listed in List 2, here you can see that I have used to for each loop on the list which contains the meta data of the user setting entity.As I have retrieved record based on the current logged in user so I will get only access the entity by 0(zero) index. Here I fist check for if the entity contains attribute or not and then check the attribute type whether attribute is pick list or lookup type because to get value of the lookup and pick list is different.If you get the value of the pick list then it will give you integer part of the pick list and we need the string value of the pick list to be displayed. Here you can see that I have get the integer value of the pick list in local variable and then search for the string value of the pick list in the list ( I have also save pick list option set in local list of the record so that I don’t need to make another MS CRM call to get option sets of the particular pick list, you can also see the code of the call back of the meta data request of the user setting entity for reference).
List 2
Next is to get the value of the lookup type, for the lookup I have just convert it to entity reference type and then get the name of the lookup. In default case I have used normal way to get the value of the other type like interger, strings etc.

Note:I have also save the type of attribute in the collection so that to make the decision of pick list and lookup to get value (for reference you can see the code of the meta data call back in the sample application).

Downloads
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!

Sunday, December 16, 2012

Reading OptionSet Items Key/value Using MetaData Request

Problem:
             Sometimes we need to get the option set key value pair to display in web resource like Silverlight etc or sometimes we have option set key but we need to display value (which is the string type display in option set).

Solution:
             When creating web resource for MS CRM 2011 there are some situations when we need to display one of the option set in combo box or in dropdown html control so that user can select any of the option from the combo box or from dropdown control. In order to get option set item we need to make meta data call which can return number of item of that field of entity.You can see the screen of the sample application in Image 1; here you can see that I have continue my last post where I have display entity attribute meta data for this sample I have displayed on option set attributes and also added data grid control to display option set items.
Image 1
Let us discuss code which is used to get the option set items from by using meta data request You can see the code in List 1 you can see the function which is used to get the items "getOptionSetLabels", function will take entity schema name and also the schema name of the option set attribute and display name of the option set attribute which will be displayed while showing busy bar.
Here you can see that first property of the request is the request name is "RetreiveAttributes" and the second property which is set is logical name of the entity, and I have used entity name which is passed to the function and then is the logical name which is the schema name of the option set attribute which is also passed to the function. Both entity name and the attribute name(which is the option set ) are schema names.
List 1

Code in List 1, I have mentioned important properties of the request, now when the request return call back event handler is executed and here you can see I just get the option set meta data to loop through and get all the option set item as code is listed in List 2.
List 2

This is how you can get option set item using meta data request, if you want to get only string value of just one option set item then you can loop through the item in the option set and then use if condition to get the specific value and then break. You can use same request to get all option set item or you can get only one value of the option set item by passing the key ( integer value of the option set).

Downloads
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!

Sunday, December 9, 2012

Retrieve Entity Attributes Metadata

Problem:
              Need to get information regarding entity as well entity attributes.
Solution:
              As in my last post I have explain how to get meta data of the entities in the MS Dynamic CRM 2011, and get the information such as display name, schema name , description of the entities , now in this post I will explain how to get attributes information of each of the entity by providing entity name and then display the display name, schema name ,description and other properties of the attributes which an entity has.You can see the layout in Image 1, here you can see that I have provided search box to enter name of the entity to search the attributes and then when user select entity name (as I have use auto complete box of Silverlight , I have also provide the entity names so that user can easily search name of the entity by entering some of the initial character of the entity name).Then in the data grid control I have display information of the attribute which an entity has.


Image 01

Code to retrieve entity attribute meta data is listed in List 1, here you can see some of the difference of the code which I have used in my last post to get information of the entities.First thing which different is that request name here I have used the "RetrieveEntity" as request name, and then I have also mention the Attributes in the entity filter property of the request so that we can get attributes of the entity.Here you can see that in the Entity Filter attribute of the request I have mention both entity and attribute mean I want entity meta data and also attribute meta data of the entity which I am requesting.Next attribute of the request is the "LogicalName" , here I have provided schema name of the entity which user has selected from auto complete box.
List 1

Next is to send the request to retrieve the entity and entity attribute meta data , in the call back method I just loop through the attributes to get the properties like display name, schema name, description etch of the attributes which will be displayed in the data grid control. In the call back method I have just used the proper type name for attribute type which are displayed in the entity customization.
Downloads
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!

Saturday, November 10, 2012

Retrieve Entities Metadata

Problem:
          Need to retrieve entities metadata information using Silverlight as web resource.

Solution:
          Sometime you need to know about the entities in the MS CRM 2011 (mean in your organization). What are the properties of the entities such as the schema name, display name, logical name , whether the entity is managed or unmanaged, whether entity is used in the displayed in advance find or not, entity object type code like account has 1 and contact has 2 and so on.For this purpose I have created small application which will list some of the useful properties of the entities. You can see the output in the Image 1 here you can see that I have displayed the display name, name( logical name ), schema name , object type code, state ( where entity is managed or unmanaged), Customizable (Boolean type either true or false) and last one the description of the entity if it is provided.

Image 1
Code is listed in List 1 , here you can see that I have used organization request object and pass the "RetrieveAllEntities" as request name. The "RetrieveAllEntities" request contains the data that is needed to retrieve metadata information about all the entities.Next is to set the "EntityFilters" attribute which take the one of the enumeration value of "EntityFilters".Here you can see that I have used the Entity which is also the default value if you don’t set the entity filter value and it is used to retrieve only entity information.Second and the last parameter which is set for the request object is the "RetrieveAsIfPublished" (Gets or sets whether to retrieve the metadata that has not been published) which take Boolean value and is required parameter. Here I have set it to false.Then created the service object and the used begin execute to past he request object, attached the callback function and also passed service object.
In the callback function you can see that first statement is to end the request which was execute request. And then get the entities from the response object here you can see that "EntityMetadata" is get from the response object.
List 1

Then I have used the foreach loop to loop through each entity and you can see that I have put if condition to filter the only those record which are either custom entity or is customizable. One more condition is check display name of the entity if it is null then continue don’t need to add entity which don’t have display name.And then used the entity properties which I want to display in the data grid control like, logical name, schema name, object type code, is managed, is customization and the description property. You can also use other properties which you want to display in the data grid control based on your requirement. Hope you get idea of how to get the entities metadata using the request object in Silverlight web resource.  

Downloads
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!

Sunday, November 4, 2012

Using WrapPanel in Silverlight 4

Problem: 
                Need to use the WrapPanel control in Silverlight 4.
Solution: 
             Like other layout control grid, stack panel or canvas etc are used to position control on specific order, in grid control you have to define the rows and columns and then set the row and column for the added control in the grid layout control, similarly you set the position of the control horizontal or vertical for the stack panel control and for the canvas you have to set the top and left position of the control which you add in the canvas, but what if you want control to arranged according to the size they are given mean if you have size of 800 by 600 layout control and you have say 100 by 100 control added and want the control o fill the 800 by 600 layout control( like stack panel if you set the orientation of the stack panel to horizontal then control are arranged horizontally and if you set the orientation to vertical then control are arranged in vertical direction but we want both if we have fix layout or resizable layout control). For this reason you can use the WrapPanel Control.
WrapPanel control will arranged child control first horizontally and if child controls are more than the width of the WrapPanel width then it will continue displaying control in new line and so on. You can see the arrangement of the control in WrapPanel in image 1. Here you can see that I have used the WrapPanel control to set the 2 column layout of child controls on left side of the user control and on right side I have used the image control to display the large image selected by user. I have also used the grid splitter control so that the size of the WrapPanel will be resized and you can see the arrangement of the control.

Image 1
The xaml code for the list box control is listed in list 1; here you can see that I have set the horizontal scrolling visibility of the list box control to disabled as I don’t want to have horizontal scrolling for my control only need vertical scrolling. Next in the list box xaml code you can see the I have define the Item template which include the image control to display image ( the width and height of the image control is set, I have set the width and height to 90 as I want to show thumbnail images) and the text block to display the image name.
List 1

Next is the xaml code for the item panel template of the list box control, here you can see that I have added the wrap panel instead of using the stack panel control or any other layout control, also I have added the FluidMoveBehaviour which is provided by the expression blend (one of the built in behavior provided by the expression blend). And set the properties of the behavior like AppliedTo to "Children" and EaseX and EaseY properties animations. I have added the behavior as when you resize the WrapPanel width using the grid splitter provider between the list box control and the full size image control then you can see the rearrangement of the child controls. 

Image 2
You can see the re-arrangement of the child control in Image 2. You can download the source code from here and experience the re-arrangement of child control within the WrapPanel control. 

All and any comments / bugs / suggestions are welcomed!
 

Sunday, October 14, 2012

Retrieve record by Using Many-To-Many Relation (Query Expression)

Problem:
              Need to retrieve related records by using many to many relations.
Solution:
              As in MS Dynamics CRM there are 1:N (one- to-many relation) , N:1 ( many- to- one relation) and there is N:N(many-to-many relation) so some time you need to get data by using one of the above relation. Using either 1: N or N: 1 relation you can easily get the record of the related entity but using N: N many-to-many relation can be little tricky.So I will show you in this post how to used one of the many-to-many relation to get the related entity details if you have such type of requirements.
As I am working on account entity in this series of posts, I have used account and marketing list many-to-many relation.I have added another data grid control for this post as I will show all related market list for the selected account entity. You can see the main screen in Image 1 , here the size of the marketing list data grid control will be less and I have just displayed the marketing list name, type(dynamic or static) and marketing list member type( the list is for account, contact or lead entity).


Note:For this post I have created separate view model for the marketing list data grid so that all the processing of the marketing list will be separated from the account data grid. You can find the new view model in the viewModel folder of the project with name “MarketingListViewModel” 
 
Image 1

Let us start with the code of how to retrieve using many-to-many relationship, the code is listed in the List 1, which you can see as below. Here you can see that first I have set the name of the entity which is used to get the record and as you can see I have set the name of the entity which is used in the many-to-many relationship (I have pick name of the entity from the many-to-many relation dialog "Relationship Entity Name", I have also upload the image of the dialog and highlighted the "Relationship Entity Name" you can see it in Image 2).Next thing is to add the condition to get the related record from the many-to-many entity; here I have passed the GUID of the account entity (entity which is selected by the user by clicking on the data grid row). So I will get all the records where account GUID will be matched( here I have set the attribute name to “entityid” which I have get by debugging the code by retrieving all the column with many-to-many entity name).
List 1

Next is the link entity which I have also discussed in my post "Retrieving Child Entity Columns Using Query Expression".here you can see that I have set the parent entity to the “Relationship entity name” as I will get ids of the list based on the entityid which is the id of the account records. So my parent entity will be the many-to-many entity name and then the parent entity attributes which will be mapped for the relationship is the id of the marketing list.After setting the parent entity name and the parent entity attribute next is to mention the child entity name and the child entity attribute, as I get to display the marketing list name and some of the information of the marketing list so my child entity will be marketing list as you can see that I have mention the LinkToEntityName and the LinkToAttributeName to marketing list name and marketing list id. Columns which I have retrieved for the linked entity are marketing list name, marketing list type (dynamic or static) and member type ( marketing list member type as marketing list is either for account , contact or lead entity).
Image 2

Add the link entity to the query expression and then send the request to retrieve the record is there is any record for the account GUID which passed.

Downloads
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!

Monday, October 8, 2012

Retrieving Child Entity Columns Using Query Expression

Problem:
                Need to retrieve child entity attributes to be retrieved along the parent entity records. So that some of the child entity record should be displayed on the data grid.

Solution:
                As the problem state that we are going to get some of child entity attribute so that we can display more detail of the child entity along the parent records. As in my previous posts regarding the query expressions I am using the “account” as main entity mean account will be my parent entity and the contact will be my child entity.For this post I have retrieve contact business phone and the parent account, I have also added two column in the data grid control to show the telephone and the parent account name ( for this post I have rearranged the columns so that primary contact information are side by side as can be seen in the Image 1).

Image 1

Let us start with the code which I have added for the link entity to be retrieved.Code is listed in List 1, Here in code you can see that I have first created the LinkEntity object first and then I have set the column name which I want to get for the link entity (the child entity). Here you can see that I have used the “telephone1” and then “parentcustomerid”, these are the two attributes which will be displayed for the child entity as additional information for the child entity.
Next is how link the parent and child entity here you can see that link entity has properties like “LinkFromEntityName” which is used to get or set the logical name of the entity that you are linking from mean the parent entity (here “account is my parent entity).After setting the name of the parent entity next is to set name of the parent attribute which will be mapped to the child entity. Here you have to set the “LinkFromAttributeName” which is used to set the name of the parent entity attribute which will be mapped from.
Next is to set the “LinkToEntityName” which in my case is the “contact” entity, mean I want to link to the “contact” entity and then set the name of the attribute of the child entity in the “LinkToAttributeName” property. Here I have set the “contactid” as the mapped attribute in the child entity.Mean I want to map the “account” “primarycontactid” which is lookup to the contact entity to be mapped with the “contact” “contactid” so that I will get telephone1 and parentcustomerid of the contact which is the then lookup to the account entity.
 
List 1


Last property of the link entity object is to set the Alias of the entity which will then used to access the attribute values in the completed call back. Here I have set the “EntityAlias” to “child”.Then at the end just add the link entity object to the query expression “LinkEntities” collections.
After adding the link entity to the query expression next is how to get the value of the link entity attributes when call back function will be called. I have list only the piece of code which is used to get the values of the linked entity, which you can see in the List 2.Here you can see that I have used the “AliasedValue” type to be getting using the GetAttibuteValue of entity and then converted to the respective type. For the phone number as it is of type string so I have used the value to convert to the Tostring. But for the EntityReference type (PrimaryAccount is of type entity reference) so the value is converted to EntityReferce type as you can see in List 2.As “AliasedValue” is “Value is of “object” type so you have to convert it to respective type before using the using vaue.
List 2

Last piece of code is the xaml code which is used to bind the two new properties, telephone is simple string type so it will be easy to bind but the Primary account is of type EntityReference so we will use the Name property of “EntityReferece” for binding as you can see in the List 3. I have also appended “(Primary Contact)” with the header text of the columns so that you can distinguish between the attributes.
List 3


That is all about how to retrieve child entity attribute and mapped the linked attribute between parent and child entity. Hope you get starting point of how to use linked entity to get addition information regarding child entity.  

Downloads
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!

Sunday, October 7, 2012

Adding Conditions in Query Expression

Problem:
              In previous post related to the query expression I have added paging in query expression to get paged data, now need to add some condition so that we get the records based on some conditions.
Solution:
              When working with data retrieval there are some condition based on which you have to fetched the records, if there is not client specific conditions then there must be at least one condition to get the active records.Image for the sample application is shown in Image 1 as shown below, here you can see that I have displayed only the active records (I have deactivated the 3 records so that I have some active and deactivate records).
Image 1

The code for the sample which I have added is shown in the List 1 , here you can see that I have first created and initialized the condition expression object and then I have set the name of the attribute which I want to compare here I have mention the “statecode” mean I want to check the is record is active or not (you have to mention the schema name of the attribute here). Then next is the name of the operator which I will use for this condition and here you can see that I have used the ConditionOperator enumeration to select the operator which I will use for this condition and I have used equal operator. At the end is the values attribute which you need to set here as I have used equal operator so I will add just one value 0 ( mean I just want to get all the active records). You can add multiple values in the Values property the operator which may need more than one value are “in” operator or “between” operator.

List 1
Next is the filter expression object which is used to add the condition which we have created just above , the filter expression class has the filter operation which will be either “or” operator or “And” operation I have used “And” operator mean no matter how many condition I will add to this filter expression object but we can just set one filter operator which will be applied on that condition if you want to set different filter operator then you have to create another filter operation object and add the condition in that filter expression object.After adding condition to the filter expression next is to add the filter expression to the query expression.
Hope you get an idea how to add condition to the query expression as conditions are the most important part of any query. You can add as many condition as you can but remember in one filter expression you can add only one filter operation which may be “Or” or “And” operation.Below are the links for the download for the source code and MS Crm 2011 solution.

Downloads
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!

Tuesday, September 25, 2012

Paging Data Using Query Expression

Problem:
               In previous post related to the query expression I have used simple query expression to get the records but now need to implement the paging by using the query expression.
Solution:
               Paging play very important role when you have large number of records. Say you have 1000 plus records or even 5000 plus records then it is better to page the data instead of showing all the records at once as it will be very difficult for the user to search in the 1000 plus record and find the record which he/she is looking in the record set.You can see the main screen as shown in Image 1, here you can see that I am shown the record information mean record start and end index like from 1 to 5 and then total number of record and the number of selected record at the bottom left of the grid( I have also attached note in the image as shown below) and then on the bottom right side I have display the current page number and then buttons for moving forward, backward and then to the first page( as there is no move to last page button in the MS CRM data grid so I have also not included that button in my grid.
Image 1
Let us now discuss the code needed for the paging in query expression. I have pasted only the related code for the paging as showing in List 1, here you can see that instead of declaring the separate object of the PageInfo I have use the query expression PageInfo object and set the properties which I will used for paging.Here you can see that first property which I set for the PageInfo object is the PageNumber property , mean which page which want to retrieved for the very first time we will send 1 as the page number ( I have used local variable to hold the current page number as page number will be changed from the backward, forward and first page buttons ). Second property which I set is the count property of the PageInfo object which is used to set the number of records per page which will be returned(I have set page size of 5 for my application you can set it to 25 or 50 whatever your requirement is).
List 1

Next property which I set is the PagingCookie, which is used for better performance when you use page and retrieve subsequent page. For the very first time query is send to retrieve the records the PagingCookie is set to null as no record is yet retrieved, when first call return it will contain the PagingCookie (the return PagingCookie is property of the EntityCollection object which you can save and then pass to the query expression if you want to move to next or previous page).The last property which I set for the PageInfo object is the ReturnTotalRecordCount property which is of type Boolean if you set it to true then it will return total number of records found for the criteria and if you set it to false then it will return -1 for total number of records for that criteria
In the callback function I have also only pasted code which is needed for the paging, rest of the code is same, code is listed in List 2 as shown below. Here you can see that first I have get the MoreRecords property of the EntityCollection object, which is very important property. which indicate whether you have more records or not for the criteria you passed.Next property of the EntityCollection object is the TotalRecordCount, which I have stored in my local variable so that to show the total number of records found for the given criteria.

Note: if you have more than 5000 plus records for the entity from where you want to display data, then TotalRecordCount property will return 5000 count not more then 5000, that is why the property MoreRecords play important role mean it will indicate that you can still query from MS CRM for more records if your paging reach the 5000.As in your MS Crm data grid you can see the total count display 5000+ and you can still query for more records.
 
List 2

Last property is the PagingCookie property of the EntityCollection object which is passed to the next query when send to retrieved the next or previous page records. 

Downloads
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!

Sunday, September 9, 2012

Showing Busy Indicator while processing Background

Problem :
               In my last post “Fetch/Retrieve Entity Using Query Expression” I have just added data grid control and on top right side on the data grid control was the refresh button which was not working, but in this post I will do some work so for refresh button and show busy indicator to the end users.

Solution :
               For this I have added the busy indicator control of the silver light and used the one of the style which I have created for the silver light busy indicator control.I have placed in the Themes folder of the project with name “BusyIndicatorStyle.xaml” dictionary.I have used same approach which is used in the post (from where I get the style of the data grid). Where he used the command to bind the refresh button so that we can perform action based on the command on the view model and command is bind in the data grid style and if you don’t have any command defined in your view model nothing will happened mean no error or exception is thrown. The delegate command class which is taken from original post of the data grid style is placed in the "Classes" folder of the project.You can see the busy indicator while fetching the records as shown in Image 1.

Image 1
Let us start with the code which I have added for the busy indicator mean the xaml and the properties which are added in the view model used to bind the is busy and busy content properties of the busy indicator control.In List 1 you can see I have pasted only the xaml related to the busy indicator control, here you can see that I have bind the IsBusy and BusyContent of the busy indicator control which are the properties in the main view model class.

List 1

In List 2 you can see that I have pasted the code which I have added for the busy indicator control and also the command of the “Refresh” button on the top right side of the data grid control. Here you can see the first property is the IsBusy of type Boolean which is bind with the IsBusy property of the busy indicator control as you can see in the List 1 (the xaml code for the busy indicator control). Here you can see the default value of the IsBusy property in the view model is false(when we send the async call to fetch the record which will set it to true which mean the busy indicator control will be shown and on call back function value is set to false mean busy indicator control will be hide).

List 2

The second property in the view model code as shown in the List 2 is the BusyContent property of type string which is bind with the BusyContent property to show the message to the user what is happening in the background. Default value of the BusyContent property is “Loading Account records…” and I have modified the message when we click the refresh button on the data grid control “Refreshing Account List...” is the message which will be shown to the user when he/she click the refresh button. Defining the property for the Busy content here is to show the message to user you can remove this property and hard code the message on the xaml end but as you have multiple operation on the view model so it is better to show different message based on operation.
The last is the command which is bind with the refresh button of the data grid control. Which is taken from the original post and here I have added my code in the function to call the getAccountList function and before that statement I have changed the value of the BusyContent property to “Refresh Account List…”.

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

Downloads
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!

Saturday, September 8, 2012

Fetch/Retrieve Entity Using Query Expression

Problem :
               Need to fetch/retrieve the entity record using the query expression.
Solution:
               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

Downloads
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!