Retrieve Duplicate Detection Rules using CRM SDK

Sometime you may require to retrieve duplicate detection rules using CRM SDK, this post will help you to write code to get duplicate detection rule. Let’s take an example we want to retrieve all the duplicate detection rules based on the entity name, if you will navigate to Settings-> Data Management -> Duplicate Detection Rules, you will see duplicate detection rules for entities, for example below is the duplicate rules for contact entity

Duplicaterule1

We can use following code to get duplicate detection rule records, we are using simplified connection to connect CRM deployment, you can refer this for details on simplified connection:

using(OrganizationService crmService = new

OrganizationService("OrganizationService"))

{

	QueryExpression query = new QueryExpression

	{

		EntityName = "duplicaterule",

		ColumnSet = new ColumnSet("name"),

		Criteria =

		{

			FilterOperator = LogicalOperator.And,

			Conditions =

			{

				new ConditionExpression

				{

					AttributeName = "baseentityname",

					Operator = ConditionOperator.Equal,

					Values = {"contact"}

				},

				new ConditionExpression

				{

					AttributeName = "statecode",

					Operator = ConditionOperator.Equal,

					Values = {1}

				}

			}

		}

	};

In above code we are retrieving only published duplicate rules, so it will fetch two records for us

Duplicaterule2

After that we can loop through entity collection to get individual duplicate rule if required using following code:


foreach (Entity duplicaterule in entityCollection.Entities){

//processing entitycollection records

}

Also in case you need unpublished duplicate rules you can check statecode like below:

new ConditionExpression
{
	AttributeName = "statecode",
	Operator = ConditionOperator.Equal,
	Values = {0}
}

One thought on “Retrieve Duplicate Detection Rules using CRM SDK

  1. Pingback: Retrieve Duplicate Detection Rules using CRM SDK - Microsoft Dynamics CRM Community

Leave a Reply