Creating Contact Record in D365 with PowerShell Script & Image Attribute

To create a Contact record in Dynamics 365 (D365) using PowerShell, including an image attribute.

Step 1. To establish the connection with D365 Run the below code and provide a valid username, password, and URL.

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

$Username="username.onmicrosoft.com"

$Password="password"

$pwd = ConvertTo-SecureString $Password -AsPlainText -Force

$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)

$CRMConnection = Connect-CrmOnline -Credential $credentials -ServerUrl "https://allamurl.crm8.dynamics.com/" -ForceOAuth

Dynamics 365

Step 2. Check the connection $CRMConnection.

 $CRMConnection

Step 3. Create an image in your local.

We prepare the binary image data from the image file located at "C:\Images\Allam.JPG".

Step 4. To convert the image content to a Base64 string execute the below code.

$numberOfContacts = 1

# Generate contact data

$contactData = @()

# Prepare binary image data
$imagePath = "C:\Images\Allam.JPG"
$imageContent = [System.IO.File]::ReadAllBytes($imagePath)

# Trim whitespace from the Base64 string
$imageBase64Trimmed = [System.Convert]::ToBase64String($imageContent).Trim()

# Convert trimmed Base64 string to byte array
$imageByteArray = [System.Convert]::FromBase64String($imageBase64Trimmed)

$contact = @{
    "firstname" = "Allam"
    "lastname" = "Purushotham"
    "entityimage" = $imageByteArray
}

$contactData += $contact

Step 5. To Create contacts execute the below code

foreach ($contact in $contactData) {
    $ContactId = New-CrmRecord -conn $CRMConnection -EntityLogicalName contact -Fields $contact
    Write-Host "Contact created: $($contact.firstname) $($contact.lastname) $($contact.annualincome)"
}

Base64 string

The image content is converted to a Base64 string and then trimmed to remove any whitespace.

The trimmed Base64 string is converted back to a byte array.

We define the contact data with the first name, last name, and the entity image byte array.

Finally, we create a new contact record in Dynamics 365 using the New-CrmRecord cmdlet from Microsoft.Xrm.Data.PowerShell module, passing the contact data.

Ensure that the provided image file path "C:\Images\Allam.JPG" is correct and that the image file exists at that location. Also, make sure that the New-CrmRecord cmdlet is executed correctly with the necessary permissions.

Step 6. Sample Code

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

$Username="username.onmicrosoft.com"

$Password="password"

$pwd = ConvertTo-SecureString $Password -AsPlainText -Force

$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)

$CRMConnection = Connect-CrmOnline -Credential $credentials -ServerUrl "https://allamurl.crm8.dynamics.com/" -ForceOAuth

# Define the number of contacts to create
$numberOfContacts = 1

# Generate contact data
$contactData = @()

# Prepare binary image data
$imagePath = "C:\Images\Allam.JPG"
$imageContent = [System.IO.File]::ReadAllBytes($imagePath)

# Trim whitespace from the Base64 string
$imageBase64Trimmed = [System.Convert]::ToBase64String($imageContent).Trim()

# Convert trimmed Base64 string to byte array
$imageByteArray = [System.Convert]::FromBase64String($imageBase64Trimmed)

$contact = @{
    "firstname" = "Allam"
    "lastname" = "Purushotham"
    "entityimage" = $imageByteArray
}

$contactData += $contact

# Create contacts
foreach ($contact in $contactData) {
    $ContactId = New-CrmRecord -conn $CRMConnection -EntityLogicalName contact -Fields $contact
    Write-Host "Contact created: $($contact.firstname) $($contact.lastname)"
}

After running the script, you can expect to see the output with your image displayed in the record below.

Output


Similar Articles