Enable Duplicate Detection

Enable Duplicate Detection

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The following code example shows how to enable duplicate detection for an entity and globally (for all entities in the organization).

This sample code can be found in the following files in the SDK download:


For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.



using System;
using MetadataServiceSdk;
using CrmSdk;

namespace Microsoft.Crm.Sdk.HowTo.DuplicateDetection
    public class EnableDuplicateDetection
        public EnableDuplicateDetection()


        public static bool Run(string crmServerUrl, string orgName)
            bool success = true;

                // Set up the CRM Services.
                MetadataService metadataService = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetMetadataService(crmServerUrl, orgName);
                metadataService.PreAuthenticate = true;
                CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
                service.PreAuthenticate = true;

                #region Setup Data Required for this Sample

                // Create a query to retrieve the organization.
                // Note: Avoid using AllColumns, and use ColumnSet with required columns only.
                //       Other columns, including FiscalSettings, do not have to be updated (these can only be set once). They are not related to this task.
                //       It is a best practice to modify only what is needed. Do not use AllColumns in an update request unless it is required.
                ColumnSet duplicateDetectionColumns = new ColumnSet();
                duplicateDetectionColumns.Attributes = new string[] {
                  "isduplicatedetectionenabled", "isduplicatedetectionenabledforimport",
                  "isduplicatedetectionenabledforonlinecreateupdate" };

                QueryByAttribute orgQuery = new QueryByAttribute();
                orgQuery.Attributes = new string[] { "name" };
                orgQuery.ColumnSet = duplicateDetectionColumns;
                orgQuery.EntityName = EntityName.organization.ToString();
                orgQuery.Values = new string[] { orgName };

                // Run the query.
                BusinessEntityCollection allOrgs = service.RetrieveMultiple(orgQuery);


                // Retrieve the organization.
                // Replace this GUID with a valid GUID for your organization in Microsoft Dynamics CRM.
                // Be aware that using AllColumns may adversely affect
                // performance and cause unwanted cascading in subsequent 
                // updates. A best practice is to retrieve the least amount of 
                // data required.
                organization theOrg = (organization)service.Retrieve(EntityName.organization.ToString(),
                         new Guid("6045ECF3-9B77-DC11-AE08-0003FFA48E24"), new AllColumns());

                // Enable globally.

                // Enable duplicate detection globally for the organization.
                theOrg.isduplicatedetectionenabled = new CrmSdk.CrmBoolean();
                theOrg.isduplicatedetectionenabled.Value = true;

                // Enable duplicate detection during data import.
                theOrg.isduplicatedetectionenabledforimport = new CrmSdk.CrmBoolean();
                theOrg.isduplicatedetectionenabledforimport.Value = true;

                // Enable duplicate detection when Microsoft Dynamics CRM for Outlook goes from offline to online.
                theOrg.isduplicatedetectionenabledforofflinesync = new CrmSdk.CrmBoolean();
                theOrg.isduplicatedetectionenabledforofflinesync.Value = true;

                // Enable duplicate detection when a record is created or updated.
                theOrg.isduplicatedetectionenabledforonlinecreateupdate = new CrmSdk.CrmBoolean();
                theOrg.isduplicatedetectionenabledforonlinecreateupdate.Value = true;

                // Create an update target.
                TargetUpdateOrganization orgUpdateTarget = new TargetUpdateOrganization();
                orgUpdateTarget.Organization = theOrg;

                // Create an update request.
                UpdateRequest orgUpdateRequest = new UpdateRequest();
                orgUpdateRequest.Target = orgUpdateTarget;

                // Update the organization settings to enable duplicate detection globally.

                // Enable for an entity.

                // Create a request to retrieve an entity.
                RetrieveEntityRequest entityRequest = new RetrieveEntityRequest();
                entityRequest.RetrieveAsIfPublished = true;
                entityRequest.LogicalName = EntityName.contact.ToString();
                entityRequest.EntityItems = EntityItems.All;

                // Execute the request.
                RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)metadataService.Execute(entityRequest);

                // Get the entity from the response.
                EntityMetadata contactEntity = entityResponse.EntityMetadata;

                // Enable duplicate detection for the retrieved entity.
                contactEntity.DuplicateDetection = new MetadataServiceSdk.CrmBoolean();
                contactEntity.DuplicateDetection.Value = true;

                // Create an update request.
                UpdateEntityRequest updateRequest = new UpdateEntityRequest();

                // Set the Entity property to the modified contact.
                updateRequest.Entity = contactEntity;

                // Execute the request.
                UpdateEntityResponse updateResponse = (UpdateEntityResponse)metadataService.Execute(updateRequest);

                // Publish the entity.
                // Note:  All customizations must be published before they can be used.
                PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
                PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)service.Execute(publishAllRequest);

                #region check success

                // Retrieve the contact entity and verify that duplicate detection is turned on.
                entityResponse = (RetrieveEntityResponse)metadataService.Execute(entityRequest);
                contactEntity = entityResponse.EntityMetadata;
                if (contactEntity.DuplicateDetection.Value != true)
                    success = false;


            catch (System.Web.Services.Protocols.SoapException)
                // Perform error handling here.
            catch (Exception)

            return success;

See Also



© 2010 Microsoft Corporation. All rights reserved.

© 2015 Microsoft