Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 51311

Deploy Dynamics CRM with PowerShell Part 1: Duplicate Detection Rules

$
0
0

Today's post is written by Ian Moore, a Developer at Sonoma Partners.

The Dynamics CRM PFE team recently released Microsoft.Xrm.Data.PowerShell, which is a PowerShell module with cmdlets for interacting with data in a CRM Organization. It includes cmdlets that allow automation of solution exports and imports, changing system settings, and administration tasks. In this series of blogs, we will look at how these cmdlets can be used to automate common tasks that are encountered in deploying solutions for Dynamics CRM. The first part will be how to enable duplicate detection rules from PowerShell.

When importing a solution that contains an entity, it is not uncommon for CRM to unpublish any duplicate detection rules for the entity.

clip_image002

This requires any affected rules to be published manually after the solution is imported. We can automate this process in a few lines of PowerShell.

First, create the following function:

This function takes in the CrmServiceClient created by the Get-CrmConnection cmdlet, and an array of guids for duplicate rules. Each guid is piped to the small “Create-PublishRuleRequest” function to create an SDK request, and the piped to the code block that executes the request and returns the response as a PublishDuplicateRuleResponse.

Now for the script, we need to make sure the module is imported and can create a connection to Dynamics CRM with Get-CrmConnection.

if (-Not (Get-Module-NameMicrosoft.Xrm.Data.Powershell)) {
     Import-ModuleMicrosoft.Xrm.Data.Powershell
}

$crmOrg = Get-CrmConnection–InteractiveMode

Before importing the solution, we can query for the duplicate rules that are currently published with Get-CrmRecords. By filtering for only active rules, we can make sure we don’t unintentionally publish a rule that wasn’t active prior to importing the solution.

$duplicaterules=Get-CrmRecords-conn$crmOrg'duplicaterule'-FilterAttributestatecode-FilterOperator -eq -FilterValue Active

$duplicateruleIds=$duplicaterules['CrmRecords'] |select-ExpandPropertyduplicateruleid

Now we have the guids we need for publishing, so we can import our solution to the organization with Import-CrmSolution, and call our publish function.

Import-CrmSolution-conn$crmOrg-SolutionFilePathC:\Path\To\Solutions\DevSolution_1_0_0.zip-PublishChanges

Publish-CrmDuplicateRules-conn$crmOrg-RuleIds$duplicateruleIds

Here is a sample script that shows the whole process.

Make sure to download the Microsoft.Xrm.Data.PowerShell module from GitHub at https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell and follow https://blogs.msdn.microsoft.com/crminthefield/ for updates to the module!


Viewing all articles
Browse latest Browse all 51311

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>