Geeks With Blogs

News This is the *old* blog. The new one is at

Elton Stoneman
This is the *old* blog. The new one is at

CRM Online is gaining a lot of traction with one of my clients, and we’re increasingly integrating it with different apps. CRM is endlessly customisable – you can add or remove fields from entities, create new entities, edit the links between entities etc. and the .NET SDK does a good job of wrapping up the generic CRM API with the specifics of your customisation. So if you’ve added a “date we got interested” field to the Account entity, the SDK exposes that as a typed property of the Account class.
There’s plenty of documentation around CRM and the SDK, but I didn’t find a good step-by-step for consuming the Organization service to create an Account and save a Quote from a back-end service, so here it is. The scenario is for integrating with CRM from a separate API, so we’re using a service identity and not a logged-in CRM user.
1. Install the Windows Identity Framework  

If you don’t already have it – the SDK tools use it for authenticating to CRM Online.
2. Download the Dynamics CRM SDK 
As well as the .NET client libraries, the SDK comes with a set of tools you’ll need to work with CRM.
2. Set up your CRM instance & account
Start with a free trial of Dynamics CRM online if you’re just exploring, and add a new account for the service identity. You’ll need to log in to CRM with the new ID and set the password, you can’t use the account with the default password. The service identity needs to be in a role to use CRM services, for dev you can give the account the Service Administrator role.
3. Create your device credentials
To connect to CRM Online externally with a Windows Live ID, you need credentials for a “device”, as well as your user. You need to register a device to get the credentials, which uses a tool from the SDK – but one you have to compile yourself. Build the deviceregistration project in sdk\tools\devieregistration, and you get an exe you can run from the command line:    

Deviceregistration.exe /operation:Register
- which will output an ID and password. Although it’s called a device ID, it’s not linked to a machine so these credentials are portable.
4. Generate your CRM service reference
The SDK comes with an extended version of SvcUtil, which can build a proxy for communicating with the CRM Organization service. CrmSvcUtil.exe is already built in sdk\bin; to generate the proxy for your instance run:    

- where:
url= the organization service url for your instance, which you can get from the CRM UI under Settings/Customizations/Developer Resources:
out – the name of the file to generate
n – the namespace for the generated code
username – your CRM service identity user name
password – your CRM service identity password
deviceid – the device id you got from step 3
devicepassword – the device password you got from step 3
servicecontextname – class name for the proxy to include a LINQ context for CRM
That will take a while, and it will generate a customized proxy for all your CRM entities – including your customizations. For my instance the generated code file came in at 175,000 lines… When you make any further customizations in CRM you’ll need to repeat that step to re-generate the proxy with your changes, so you could look to incorporate that into a pre-build step in your project.
5. Add references
In your VS project, add references to the SDK assemblies -

- and add your generated proxy code to the project.
6. Go!
You’re ready to start coding against the OrganizationServiceProxy class, which wraps up the service calls.

I’ve got a gist here: Saving a quote with the Dynamics CRM SDK, which has the full code to create an account, primary contact and a quote, and link them all together.
Main points – you need to specify your device and service identity credentials for the call:

  var devicecredentials = new ClientCredentials(); 
  devicecredentials.UserName.UserName = "YOUR-Crm.DeviceId"); 
  devicecredentials.UserName.Password = "YOUR-Crm.DevicePassword"); 

  var credentials = new ClientCredentials(); 
  credentials.UserName.UserName = "YOUR-Crm.UserName"); 
  credentials.UserName.Password = "YOUR-Crm.Password"); 
  var organizationUri = new Uri("YOUR-Crm.OrganizationServiceUrl")); 

  using (var serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, devicecredentials))

Then using the LINQ provider, you need to save the account and then re-attach it to the context before you try and add links to other entities, so we have the saved ID of the account:

  using (var context = new CrmContext((IOrganizationService)serviceProxy)) 
      var account = new Account(); 
      account.Name = "FirstName LastName"; 

Account entities only have name as a mandatory field; Contacts need name; Quotes need an Id – which is your own unique ID, CustomerId and a PriceLevelId – which come from entity references. In the sample code, I just use the first pricing set I find from CRM (which I created in the UI).

Posted on Friday, August 16, 2013 7:12 AM | Back to top

Comments on this post: Integrating with Dynamics CRM Online: Saving a Quote with the SDK

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Elton Stoneman | Powered by: