Swimburger

Use PowerShell to communicate with Dynamics CRM using the .NET XRM SDK

Niels Swimberghe

Niels Swimberghe - - Dynamics

Follow me on Twitter, buy me a coffee

CRM and PowerShell logo

If you have developed client applications or plugins for Dynamics CRM, you likely are familiar with the CRM/XRM libraries. You may have gotten those DLL's from the CRM installation, the SDK zip, or the NuGet package. Another way to interact with these CRM DLL's is through PowerShell. PowerShell is built upon .NET meaning you can call the same CRM libraries from PowerShell as from .NET applications.

The easiest way to start using PowerShell with CRM libraries, is to install the "Microsoft.Xrm.Tooling.CrmConnector.PowerShell" PowerShell module. Note all the DLL's that come with this PowerShell module which you can find on the NuGet page under 'FileList':

PowerShell Module FileList containing many CRM DLL's

This module contains many DLL's but most importantly, the core CRM DLL's you usually use such as:

  • Microsoft.Xrm.Sdk.Deployment.dll
  • Microsoft.Xrm.Sdk.dll
  • Microsoft.Xrm.Sdk.Workflow.dll
  • Microsoft.Xrm.Tooling.Connector.dll

Use the following command to install the PowerShell module:

Install-Module -Name Microsoft.Xrm.Tooling.CrmConnector.PowerShell

The PowerShell module only provides two PowerShell functions:

Although the module only provides these two PowerShell functions, the entire XRM SDK is made available to you.

For example, here's a PowerShell script file named "GetOptionSet.ps1" that will print out all optionset values and labels for a given Entity + OptionSet-Attribute:

Param(
    [Parameter(Mandatory=$true)]
    [String]
    $ConnectionString,
    [Parameter(Mandatory=$true)]
    [String]
    $EntityLogicalName,
    [Parameter(Mandatory=$true)]
    [String]
    $OptionSetAttributeName
)

# Import the Dynamics XRM PowerShell module
# https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell/3.3.0.887
Import-Module Microsoft.Xrm.Tooling.CrmConnector.PowerShell;
# Get a CrmServiceClient to communicate with Dynamics CRM
$CrmClient = Get-CrmConnection -ConnectionString $ConnectionString;

# Create a RetrieveAttributeRequest to fetch Attribute metadata
$AttributeRequest = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest]::new();
$AttributeRequest.EntityLogicalName = $EntityLogicalName;
$AttributeRequest.LogicalName = $OptionSetAttributeName;
$AttributeRequest.RetrieveAsIfPublished = $True;
$AttributeResponse = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse]$CrmClient.Execute($AttributeRequest);

# Get the Value/Label pairs and print them to the console
$AttributeResponse.AttributeMetadata.OptionSet.Options `
    | Select-Object -Property `
        @{Name = "Value"; Expression={$_.Value}},`
        @{Name = "Label"; Expression={$_.Label.UserLocalizedLabel.Label}};

# Close the connection to CRM
$CrmClient.Dispose();

To use the script, open a PowerShell window and invoke the PowerShell file as below:

# Follow the link below to learn how to create your connectionstring:
# https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/xrm-tooling/use-connection-strings-xrm-tooling-connect#connection-string-parameters
$CrmConnectionString = "YOURCONNECTIONSTRING";

.\GetOptionSet.ps1 `
    -ConnectionString $CrmConnectionString `
    -EntityLogicalName "lead" `
    -OptionSetAttributeName "statuscode";

# Output will look something like this:
#   Value Label
#   ----- -----
#       1 New
#       2 Contacted
#       3 Qualified
#       6 Not Interested
#       5 Unable to Contact

Using PowerShell, you can provide a lot of value with a single file as opposed to .NET projects which require at least multiple files, folders & compilation. I still prefer creating .NET projects for larger endeavors, but PowerShell has been very helpful for scripting things quickly. Being able to pass single script files between machines and colleagues has also been very handy.

In the end you can achieve anything with either PowerShell or .NET projects, so whatever floats your boat!

Related Posts

Related Posts