CRM SDK – Using Detect Duplicates Settings in Code

In CRM 2011, if you’ve set up a bunch of Duplicate Detection rules, you may want to use these same rules from a plugin or just any general application that utilised the CRM Sdk.

To do this, make sure the duplicate detection settings have been published (see screenshot below):

Then, in code, you can use these duplicate detection settings implicitly by passing in an entity which you’ve not get saved to a method like the following:

/// <summary>
/// Checks for duplicate Guid
/// </summary>
/// <param name="account"></param>
/// <returns>First duplicate account id, if any duplicates found, and Guid.Empty if not</returns>
public Guid DuplicateExists(Account account)
    RetrieveDuplicatesRequest request = new RetrieveDuplicatesRequest();
    request.BusinessEntity = account;
    request.MatchingEntityName = Account.EntityLogicalName;
    request.PagingInfo = new PagingInfo();
    request.PagingInfo.PageNumber = 1;
    request.PagingInfo.Count = 1;
    RetrieveDuplicatesResponse response = (RetrieveDuplicatesResponse)ServiceProxy.Execute(request);
    return response.DuplicateCollection.Entities.Count > 0 ? response.DuplicateCollection.Entities[0].Id : Guid.Empty;

You’ll notice that if you switch off the duplicate detection rule in CRM then this code will not find the duplicate, and if you switch it back on then the duplicate will be detected again. Likewise if you change the definition of the duplicate. This is great if you want to ensure that applications and users are conforming to the same set of duplicate detection rules.

How to Generate C# (or VB.NET) Class for CRM 2011 Organisation

When developing against a customised instance of CRM 2011, often you will want to do so against a strongly typed instance of the current CRM 2011 organisation.

To do this, you need to use the crmsvcutil.exe application which will create the a strongly-typed version of the CRM organisation as a separate set of classes.

You’ll find the application in the CRM SDK bin subfolder.

There are a number of options you can see by running

crmsvcutil /?

from a command prompt.

This is what you’ll see:

Full set of options available in the CRM 2011 crmsvcutil utility

CRM 2011 crmsvcutil options

To find your organisation Url, go open up your CRM 2011 organisation, and navigate to the Settings -> Customisations -> Developer Resources area as follows:

How to find the CRM 2011 Developer Resources

Once you’re there, the organisation url can be found by selecting the Organisation Service, as shown in the screenshot below:

How to work out the Dynamics CRM 2011 Organisation Web Service Url

To create the proxy class, enter this into a command prompt from the SDK bin folder:

crmsvcutil /url:http://localhost/[ORGANISATION-SHORT-NAME]/XRMServices/2011/Organization.svc /namespace:MyNameSpace.Here /out:"c:\Projects\MyProject\[ORGANISATION-NAME].cs"

Your machine will whir for a bit and then you’ll see the new (5Mb+!) C# file that has been generated.

To use the file, create a new Visual Studio project, add references to the Microsoft.Crm.Sdk.Proxy.dll and Microsoft.Xrm.Sdk.dll (found in the SDK bin folder) and then you can start developing against the strongly typed CRM object model.