CRUD with Microsoft Dynamics CRM 2011

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:

crmsvcutil.exe
/url:https://<orgname>.crm5.dynamics.com/XrmServices/2011/Organization.svc
/out:C:GeneratedCode.cs
/serviceContextName:CRM2011DataContext
/namespace:Microsoft.CRM2011
/username:<username>
/password:<password>

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).

For example:

<add key=ServerAddressvalue=<yourorg>.crm5.dynamics.com/>
<add key=OrganizationNamevalue=<crmorganisationname>/>
<add key=SSLvalue=true/>
<add key=UseDefaultADCredentialsvalue=false/>
<add key=Domainvalue=“”/>
<add key=UserNamevalue=“<liveid>@live.com/>
<add key=Passwordvalue=“<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.

Trouble shooting

If you get an error stating could not load file or assembly ‘Microsoft.IdentityModel, Version=3.5.0.0…’

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.

 

Rami Mounla

2 thoughts on “CRUD with Microsoft Dynamics CRM 2011

  1. I wonder, how could I use custom attributes.
    I made the coordinates and attributes when using the CRM automatically generates, it works! When I use custom attributes that are, I can not use because an error on page pops!

    graciously
    Filipe Mateus

Leave a Reply

Your email address will not be published. Required fields are marked *