In this second tutorial, we’ll cover different ways of querying Microsoft Dynamics CRM 2011. The sample command line solution contains query examples using fetchXML, QueryExpression, LINQ (late bound and early bound).
The solution also covers complex LINQ queries with join statements and conditions.The only two prerequisites that you need (other than the obvious Visual Studio 2010) are a Microsoft Dynamics CRM 2011 instance (preferably hosted on the cloud) and the Microsoft Dynamics CRM 2011 SDK. If you don’t have a CRM 2011 instance, feel free to register for a free 1 month trial http://crm.dynamics.com/
You can download the CRM 2011 SDK from http://www.microsoft.com/download/en/details.aspx?id=24004. The SDK contains the crmsvcutil.exe tool that will generate your context.
The sample solution called CRM2011LINQ.zip can be downloaded from my shared TechEd 2011 SkyDrive.
To run the solution you’ll need to follow the 2 steps below:
Step 1 Generate the context file
You can find the crmsvcutil.exe in the CRM 2011 SDK, under the bin folder. When you execute the command line, set “CRM2011DataContext” as the serviceContextName, and “Microsoft.CRM2011” as the namespace. Don’t forget to type in the correct CRM2011 URL and your username and password.
This example is for an online CRM 2011 instance:
Once your code is generated, add it to your solution as an existing file.
Step 2 Update the app.config
In Visual Studio update app.config to contain the correct CRM information.
The ServerAddress is the same URL that you use to log in to CRM (omit the http).
The OrganisationName can be found by navigating to Setting -> Customizations -> Developer Resources “Organization Unique Name” in CRM (diagram below).
<add key=“ServerAddress” value=“<yourorg>.crm5.dynamics.com“/>
<add key=“OrganizationName” value=“<crmorganisationname>“/>
<add key=“SSL” value=“true“/>
<add key=“UseDefaultADCredentials” value=“false“/>
<add key=“Domain” value=“”/>
<add key=“UserName” value=“<liveid>@live.com“/>
<add key=“Password” value=“<password>“/>
Step 3 Update the GUID
In the ComplexLinqExampleId method in the Program.cs file, the AccountId is hard coded. This GUID is specific to my environment and it has to be changed to a value specific to yours otherwise the query will not return any values.
Now that you have set up the solution, you are ready to launch it and view the results.
It might help if you have sample data in your CRM 2011 instance.
If you get an error stating could not load file or assembly ‘Microsoft.IdentityModel, Version=220.127.116.11…’
You need to download the windows identity foundation from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17331
I noticed that some people are having issues with my SkyDrive. If you have having any problems connecting, drop me a comment and I’ll send you an alternative link.