Our guest blogger is Microsoft .NET MVP and Regional Director David Yack who is the author of a number of books including the very popular CRM as a Rapid Development Platform. David blogs at http://crm.davidyack.com/.
Yesterday, Microsoft released CRM SDK 4.0.12 - before you just download it and think its just another minor update – read on. This update includes what is being referred to as Advanced Developer Extensions. Before you stop reading saying “I’m not advanced” – “Advanced” I believe is marketing’s way of saying this builds on top of the strong foundation that CRM already provided – not that you need to be a “Super Advanced” developer to use it.
In reality, it’s advanced because it’s going to simplify how you interact with the CRM data. More specifically, it will allow you to use the LINQ expression syntax that developers have been using since C#3/VB9 to build queries. Additionally, this introduces the idea of using the normal .NET data types instead of the CRM specific ones e.g CRMBoolean. In the past, CRM had specific types for CRM Boolean because at the time .NET didn’t support nullable types.
There are other good things in the new SDK that I will try to cover in future blog posts – but wanted to make sure people didn’t gloss over this new feature.
Dave’s 10 Minute Quick Start Walkthrough - (SDK download times excluded of course, I can’t help if you have a slow connection!)
1 – Download the updated CRM SDK
2 – Create a new console application in Visual Studio 2008 or 2010 – choose .NET 3.5 as your framework
3 – Create a folder in the new project called Entities – Right click Open Folder in Browser as you can see below
4 – Copy the path name from Windows Explorer – you will need it to provide to the CrmSVCUtil program so it can use it to create the generated classes
5 – From a command line – go to the SDK folder/Microsoft.xrm/Tools and run the following command line – replacing MyCrmServer with your actual server name, replace MyCrmOrg with your actual CRM Org and MyEntitiesFolder with the name you copied in step 4. Note : This assumes using integrated authentication for on-premise – support is available for many different scenarios – check the docs.
crmsvcutil /server:http://MyCrmServer/MyCrmOrg /out:MyEntitiesFolder
This utility will access the server metadata and build classes for each of the entities and place them in your project folder.
6 – Right click – Add Existing Item on the entities folder, Make sure the browse dialog is in the Entities folder and select all the files and click OK
7 – Add references to the CRM SDK assemblies – using Add Reference – Browse and choosing the following
8 – Add references to the Xrm assemblies from the SDK/Microsoft.xrm/bin folder as you see in the following
9 – Add references to the following .NET assemblies
10 – Build your application you should get a clean build at this point – before we add more code
11- Open the app.config file and add the following connection string section. Modify the server name to match your server name, and the org name to match your org name - if you don’t have an app.config – just add one via add-new items:
<add name="mycrm" connectionString="Authentication Type=Integrated;
12 – In the Program.cs file - Add a Using statement for your entities as you can see below:
13 – The new API uses a concept of a Data Context – you can think of this as the gateway to working with the CRM data – In the Main method add the following – which creates an instance and references the connection string we added to the app.config:
DataContext ctx = new DataContext("mycrm");
14 – Use LINQ to compose a query of accounts. The following defines the query but does not execute it – with LINQ expressions they get executed when used as we will see later. The following builds a query looking for all accounts that have an “a” in the name.
var query = from acct in ctx.accounts
15 – Loop though each of the accounts and print the name – This will cause the actual execution of the query against CRM – Also notice that we can use strongly typed properties no guessing the names of magic strings:
foreach (var acct in query)
Console.WriteLine("Account Name :" + acct.name);
17 – Run the application you should get a list of accounts
18 – See how easy it is to add ordering to a query – add the following to the LINQ query between the Where and the Select
19 – Run the application again to see names sorted
20 – Let’s add a record – Adding a record is also very easy – First create an instance of the account object and set the minimum – This uses the account class that was generated by CrmSvcUtil earlier:
var newAccount = new account();
newAccount.name = "test account";
21 – Add the record to the Data Context – This step advises the context about the new record – but it doesn’t cause the create to happen on the server yet. This means we can do multiple adds / updates and then send them to the server when ready.
22 – Call the SaveChanges method on the context to cause the records to be created on the CRM server:
Note : This will really add a record to your server – you have been warned!
Updating is just as easy – modify a record you retrieved from a query and then call UpdateObject(account) to notify the Data Context of the changes. And of course DeleteObject(account) will make that account vanish for ever –a great way to get rid of pesky accounts :) Like Add, Update and Delete do not get sent to the server until SaveChanges is called.