Wednesday, July 27, 2011

Json and Silverlight with ODataService in CRM 2011

Dynamics CRM 2011 now provides the ability to perform data operations on CRM entities using JSON and ODATA service.

Here we have tried to explain the various clauses of JSON that is supported by CRM ODATA and how this service can be used in Silverlight as well as in Scripts. Note not all standard ODATA clauses are supported by CRM.

To use these clauses in silverlight you need to add a reference to the ODataService of the CRM i.e. OrganizationData.svc.

$select: This clause is the Select part of any SQL statement where you can specify the list of attributes to be returned by the query. Note you can also provide the collection property of a 1:N or N:1 relationship.

Using JSON

If you write the following URL to read the opportunity.

http://://xrmservices/2011/organizationdata.svc/OpportunitySet

it will return all columns of the opportunity in XML but if you want to read only the name you can add the select as follows.

http://ad12:5555/crmorg1//xrmservices/2011/organizationdata.svc/OpportunitySet?$select=Name

It will return the folliwng XML



Using Silverlight:

To use the $select in the Silverlight app you need to write the query as follows.

DataServiceQuery query = (DataServiceQuery_context.OpportunitySet.AddQueryOption("$select", "Name");


$expand: Directs that related records should be retrieved in the record or collection being retrieved.

Using JSON:
If you want to read the customer information with the opportunity while we are reading the opportunity by JSON then we can use the expand, and then we can read the customer details with the opportunity. Below we have given the JSON url.

http://://xrmservices/2011/organizationdata.svc/OpportunitySet?$expand=opportunity_customer_accounts,opportunity_customer_contacts&$select=*,opportunity_customer_accounts,opportunity_customer_contacts

Using Silverlight:

To use this with the silverlight you need to write the following query in the silverlight.

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$select", "*,opportunity_customer_accounts,opportunity_customer_contacts")
.AddQueryOption("$expand", "opportunity_customer_accounts")
.AddQueryOption("$expand", "opportunity_customer_contacts");

$filter:

Using JSON:
To set the condition in the JSON query we can use the $filter clause in the url.
Suppose if you want read all accounts of an user the JSON url will be as follows.

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$filter=OwnerId/Id eq (guid'34fca7f5-d556-e011-b9a8-00155d005515')

Using Silverlight:
In silverlight we can use the AddQueryOption to add the $filter as we use for $expand or we can use the Where Condition as shown in the below.

DataServiceQuery query = (DataServiceQuery)_context.AccountSet.Where(a => a.OwnerId.Id == new Guid("34FCA7F5-D556-E011-B9A8-00155D005515"));

To do it through AddQueryOption you can generate the filter in a string and then pass that string a parameter to the filter option. This provides us to create dynamic filters as shown below


filter += "new_testId eq (guid'" + _userid + "') ";

DataServiceQuery query = (DataServiceQuery)

query = query.AddQueryOption("$filter", filter);

$orderby: We can use it to set the sort order. Use it same as $filter.

Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$orderby=Name, Address1_Country.

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$orderby", "Name, Address1_Country")


$top: To define the range that how many record want to read. Max limit is 50.

Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$top=10

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$top", 10)


$skip: To define the that how many record want to skip.
Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$skip=10

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$skip", 10)


NOTE: By default any query executed using ODATA service will only return 50 records at a time. If we want to display All records that exist even if it is more than 50 records, then you can use a combination use of the $skip and $top clauses to read all records.

For this you can recursively call this Query where _top will be const with value 50. And at each recursion we can increase the value of the _skip by 50.

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$skip", _skip).AddQueryOption("$top", _top)

Friday, July 1, 2011

How to Enable and Disable the most recently used items for lookup field only.

In CRM 2011, we have found the new feature for the lookup type fields that you have the ability to see the most recently used items for that lookup while creating the records for entity.
Also you can disable this feature as well by default it is enable for all lookup types fields. If you like to see those items then, during creation of record, you need to “Double Click” on the lookup field.

See the below example for details:
• Below we have shown the account form which has the lookup field “Parent Location” and “Disable most recently used items” option is unchecked for this field as shown in below screen shot.


When you “Double Click” on Parent location field it will show you the below result.



To disable this feature you need to check the “Disable most recently used items” option then Save and publish the Form. It will disable above feature.

Thanks!

Sunday, May 29, 2011

How to get the Total Results returned Record Count for a given query

In CRM2011 it is now possible to get the Total count of records returned by a query using Fetch XML.

Using Query expression:

You can set the ReturnTotalRecordCount property to True.



Using FetchXml:

You can set the same property as shown below.

<fetch mapping='logical' count='10' returntotalrecordcount='true'>
<entity name='account'>
<attribute name='accountid' />
<attribute name='accountnumber' />
<attribute name='address1_city' />
<attribute name='address1_telephone1' />
<attribute name='name' />
<order attribute='name' />
<filter>
<condition attribute='address1_city' operator='like' value='a%' />
</filter>
</entity>
</fetch>

This can be helpful to show Record 1 of kind of status labels.

Hope this helps!

Wednesday, May 11, 2011

How to remove the default ISV buttons of CRM 4.0 from CRM 2011 after upgrade

Once you upgrade from CRM 4.0 to CRM 2011, you would find a new menu item ISV showing up in the CRM 2011 menu bar.

You never saw them before in CRM 4.0… that’s because by default CRM disables the showing up of these buttons in CRM 4.0. These buttons come along with CRM 4.0 to display the features and functionality of adding custom button.

Now you see them in CRM 2011 and you want that taken off…

Listed below are the steps to remove these buttons from your system.
- Create new solution which only contain the Application Ribbon component
- Now export this solution and extract the exported zip on your machine
- Open customization.xml file in edit mode
- You will find a similar code as shown below in the customization file


- You need to remove the Custom actions section so that this file now includes only the following nodes.

- Save the customization.xml file. Zip the file and import this customization.
- Refresh CRM to find the Menu items taken off.

Monday, May 2, 2011

How to pass field values to Form

Sometimes you have requirement to open the particular type of new record with some default values already filled. This can be achieved by passing the field values to the query string.

To open the new record form

https://<ServerUrl>/main.aspx?etn=<entity type code>&pagetype=entityrecord

Pass field values as query string parameters

http://<ServerUrl>/main.aspx?etn=<entity type code>&pagetype=entityrecord&extraqs=<fieldschemaname>=<value>

If you need to pass the multiple field values then just append those field-value combination string at the end with the “&” as a separator.

Note while generating the URL you need to take care of some characters. Instead of directly placing the character you need to use the ASCII code for that character. Below table will provide you with the list of mostly used characters while creating URL. You should prefix the “%” character before the ASCII code.


Now below example will give you an idea on how to generate URL for different type of fields, the name of the field (i.e. schema name) must be specified in lower case only.

(Blue is the actual field name and its related attribute name like for lookup there are 2 names and brown is the actual value)

Below is the total URL with all above field values passed to the new contact form,
"http://<ServerName>/main.aspx?etn=contact&pagetype=entityrecord&extraqs=birthdate%3D01%2F14%2F1984%26salutation%3DMr%26gendercode%3d1%26creditonhold%3d1%26defaultpricelevelid%3dD0B854EC-1E1A-E011-8D2B-D8D3855B354B%26defaultpricelevelidname%3dComputer%20Stationary%26parentcustomerid%3D%7b0D98B15F-0565-E011-8D90-1CC1DEE8CEB7%7d%26parentcustomeridname%3DInogic%26parentcustomeridtype%3Daccount"

Hope this helps!