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!
 

Friday, September 7, 2012

Using Query Expression in Silverlight Web Resource

During my work on MS CRM 2011 I have most of my work is on Silverlight web resource and here in this post I will list how to use the query expression in your Silverlight web resource. This will be series of article which will explain difference scenarios regarding question expression.First I will start with basic of the query expression and will guide you how to add create CRM grid in Silverlight web resource.

  1. Fetch/Retrieve Entity Using Query Expression 
  2. Showing Busy Indicator while processing Background 
  3. Paging Data Using Query Expression
  4. Adding Conditions in Query Expression   
  5. Retrieving Child Entity Columns Using Query Expression
  6. Retrieve record by Using Many-To-Many Relation (Query Expression)
  7. Reading User Settings by User Id
  8. Reading Organization Settings
I will keep update this post as I write new post related to the query expression.

Metadata retrieval
  1. Retrieving Entity Metadata 
  2. Retrieving Entity Attribute Metadata  
  3. Reading OptionSet Items Key/value Using MetaData Request   
Note: The data grid style used in this example is taken from following link

All and any comments / bugs / suggestions are welcomed!