Dynamics CRM: Create Duplicate Detection Rules using CRM plugin

What is duplication detection rules?

To keep up the trustworthiness of your information, it's a smart thought to have rules set up to diminish copy records in the framework. Microsoft Dynamics CRM incorporates default copy recognition rules for records, contacts, and leads, however not for different sorts of records. On the off chance that you need the framework to identify copies for other record sorts, you'll have to make another principle.

Why did I create duplicate detection rules via plugin?

Each time I get a new client, I have to setup new CRM site. So each time I have repeat number of step like creating duplication detection rules, bulk delete operations. So I have create plugin that create duplication rules. So I can include this plugin into my solution and deployed into new CRM.

Features of this solutions

This plugin will be execute when "publishAll" event triggered.  And code level im checking that plugin is already run or not. If it is run, plugin will not execute furthermore. To verify this I have create custom entity called "parameters" and that entity having a property called "IsDuplicateRulePluginRun". Once plugin ran it will update this property. You can define your rules in xml file. Anytime you can add new rules without changing plugin code. Xml file will be uploaded to web resource. 

using System;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
  
namespace Crm.Core
{
  
 public class  DuplicateRulePlugin : IPlugin
 {
  private Guid _ruleId;
  
  
  public class  CustomDetectionRule
  {
   public string  name { get; set; }
   public string  baseentityname { get; set; }
   public string  matchingentityname { get; set; }
  
   public Condition Conditions { get; set; }
  }
  
  public class  Condition
  {
   public List<RuleCondition> RuleConditions = new List<RuleCondition>();
  }
  
  public class  RuleCondition
  {
   public string  baseattributename { get; set; }
   public string  matchingattributename { get; set; }
   public string  operatorcode { get; set; }
  }
  
  private List<CustomDetectionRule> GetCustomerList(string xmlData)
  {
   XElement xmlDoc = XElement.Parse(xmlData);
   var rules = from rule in  xmlDoc.Descendants("Rule")
      select new  CustomDetectionRule
      {
       name = rule.Element("name").Value,
       baseentityname = rule.Element("baseentityname").Value,
       matchingentityname = rule.Element("matchingentityname").Value,
       Conditions = new  Condition()
       {
        RuleConditions = new  List<RuleCondition>(from cond in rule.Descendants("Condition")
                   select new  RuleCondition
                   {
                    baseattributename = cond.Element("baseattributename").Value,
                    matchingattributename = cond.Element("matchingattributename").Value,
                    operatorcode = cond.Element("operatorcode").Value
                   })
       }
      };
   return rules.ToList();
  }
  
  public void  Execute(IServiceProvider serviceProvider)
  {
   Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
  
   if (context.MessageName.Equals("PublishAll"))
   {
    IOrganizationServiceFactory serviceFactory =
    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
  
    string isPluginRun=string.Empty;
    Guid paraId = Guid.Empty;
    QueryExpression queryRun = new  QueryExpression("new_parameter");
    queryRun.ColumnSet.AddColumns("new_name", "new_parameterid", "new_isduplicaterulepluginrun");
    queryRun.Criteria = new  FilterExpression();
    queryRun.Criteria.AddCondition("new_name", ConditionOperator.Equal, "IsDuplicateRulePluginRun");
      
  
    EntityCollection resultRun = service.RetrieveMultiple(queryRun);
    foreach (var a in resultRun.Entities)
    {
     isPluginRun = a.GetAttributeValue<string>("new_isduplicaterulepluginrun");
     paraId = a.GetAttributeValue<Guid>("new_parameterid");
    }
  
    if (isPluginRun.Equals("0"))
    {
  
     string _xmlData = string.Empty;
  
     QueryExpression query = new  QueryExpression("webresource");
     query.ColumnSet.AddColumn("content");
     query.Criteria = new  FilterExpression();
     query.Criteria.AddCondition("name", ConditionOperator.Equal, "new_DuplicationRuleXml");
  
     EntityCollection result = service.RetrieveMultiple(query);
     foreach (var a in result.Entities)
     {
      _xmlData = a.GetAttributeValue<string>("content");
     }
  
     _xmlData = DecodeFrom64(_xmlData);
     List<CustomDetectionRule> temp = GetCustomerList(_xmlData);
  
     foreach (var a in temp)
     {
      Entity duplicateRule = new  Entity("duplicaterule");
      duplicateRule["name"] = a.name;
      duplicateRule["baseentityname"] = a.baseentityname;
      duplicateRule["matchingentityname"] = a.matchingentityname;
  
      _ruleId = service.Create(duplicateRule);
  
      List<RuleCondition> cond = a.Conditions.RuleConditions.ToList<RuleCondition>();
      foreach (var b in cond)
      {
       Entity duplicateRuleCondition = new  Entity("duplicaterulecondition");
       duplicateRuleCondition["baseattributename"] = b.baseattributename;
       duplicateRuleCondition["matchingattributename"] = b.matchingattributename;
       duplicateRuleCondition["operatorcode"] = new  OptionSetValue(int.Parse(b.operatorcode));
       duplicateRuleCondition["regardingobjectid"] = new  EntityReference("duplicaterule", _ruleId);
  
  
       Guid conditionId = service.Create(duplicateRuleCondition);
      }
      // Execute the publish request.
      Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleResponse response =
       (Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleResponse)service.Execute(new Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest() { DuplicateRuleId = _ruleId });
        
     }
     Entity new_parameter = new  Entity("new_parameter");
     new_parameter["new_parameterid"] = paraId;
     new_parameter["new_isduplicaterulepluginrun"] = "1";
     service.Update(new_parameter);
  
    }
      
   }
  }
  
  // Decode the file to the readable format
  public string  DecodeFrom64(string encodedData)
  {
   byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
   string returnValue = System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
   return returnValue;
  }
 }
}








<?xml version="1.0" encoding="UTF-8"?>
<Rules>
 <Rule>
  <name>Duplicate Rule using XML sample 1</name>
  <baseentityname>contact</baseentityname>
  <matchingentityname>contact</matchingentityname>
  <Condition>
   <baseattributename>firstname</baseattributename>
   <matchingattributename>firstname</matchingattributename>
   <operatorcode>0</operatorcode>
  </Condition>
  <Condition>
   <baseattributename>lastname</baseattributename>
   <matchingattributename>lastname</matchingattributename>
   <operatorcode>0</operatorcode>
  </Condition>
 </Rule>
 <Rule>
  <name>Duplicate Rule using XML sample 2</name>
  <baseentityname>contact</baseentityname>
  <matchingentityname>contact</matchingentityname>
  <Condition>
   <baseattributename>emailaddress1</baseattributename>
   <matchingattributename>emailaddress1</matchingattributename>
   <operatorcode>0</operatorcode>
  </Condition>
 </Rule>
</Rules>