Managing Columns in a Dataverse Table using PowerShell

Loading

Microsoft Dataverse provides a structured way to store business data. Using PowerShell, you can efficiently manage columns (also called attributes or fields) in a Dataverse table. This guide explains how to list, create, update, and delete columns in a Dataverse table using PowerShell.


Step 1: Prerequisites

1. Required Permissions

  • You need System Administrator or Power Platform Admin role in Dataverse.
  • The Microsoft Dataverse API must be enabled for your environment.

2. Install and Import PowerShell Modules

To interact with Dataverse, install and import the necessary PowerShell modules.

# Install Power Platform Administration module
Install-Module -Name Microsoft.PowerPlatform.Administration -Scope CurrentUser -Force

# Install Dataverse Client module
Install-Module -Name Microsoft.PowerPlatform.Cds.Client -Scope CurrentUser -Force

# Import modules
Import-Module Microsoft.PowerPlatform.Administration
Import-Module Microsoft.PowerPlatform.Cds.Client

Step 2: Connect to Dataverse

Option 1: Interactive Login (Prompt for Credentials)

# Connect to Dataverse interactively
$connection = Connect-CdsService -ConnectionString "AuthType=OAuth;Url=https://yourorg.crm.dynamics.com;Prompt=Login"

A sign-in window will appear, prompting you to log in with your Microsoft 365 admin credentials.

Option 2: Using Service Principal (App Registration)

For automated scripts, use an Azure AD App Registration.

# Define connection details
$clientId = "your-app-client-id"
$clientSecret = "your-app-client-secret"
$tenantId = "your-tenant-id"
$orgUrl = "https://yourorg.crm.dynamics.com"

# Create authentication context
$secureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($clientId, $secureSecret)

# Connect to Dataverse
$connection = Connect-CdsService -Url $orgUrl -ClientId $clientId -ClientSecret $secureSecret -TenantId $tenantId

Step 3: List All Columns in a Dataverse Table

Before making changes, list all columns in a specific table.

# Define the table name
$tableName = "new_CustomerData" # Replace with your table’s logical name

# Retrieve columns for the specified table
$columns = Get-CdsTableColumn -Connection $connection -EntityLogicalName $tableName

# Display columns
$columns | Select-Object LogicalName, DisplayName, AttributeType

Step 4: Add a New Column to a Dataverse Table

You can create different types of columns in a Dataverse table.

1. Add a Text Column (Single Line of Text)

# Define column properties
$columnName = "EmailAddress"
$columnDisplayName = "Email Address"

# Create text column
$column = New-Object Microsoft.Xrm.Sdk.Entity("AttributeMetadata")
$column["LogicalName"] = $columnName
$column["DisplayName"] = @{ "LocalizedLabels" = @( @{ "Label" = $columnDisplayName; "LanguageCode" = 1033 }) }
$column["AttributeType"] = "String"
$column["MaxLength"] = 255

# Add column to table
New-CdsTableColumn -Connection $connection -EntityLogicalName $tableName -Column $column
Write-Host "Column '$columnDisplayName' added to table '$tableName'."

2. Add a Number Column (Integer Type)

# Define column properties
$columnName = "CustomerAge"
$columnDisplayName = "Customer Age"

# Create number column
$column = New-Object Microsoft.Xrm.Sdk.Entity("AttributeMetadata")
$column["LogicalName"] = $columnName
$column["DisplayName"] = @{ "LocalizedLabels" = @( @{ "Label" = $columnDisplayName; "LanguageCode" = 1033 }) }
$column["AttributeType"] = "Integer"

# Add column
New-CdsTableColumn -Connection $connection -EntityLogicalName $tableName -Column $column
Write-Host "Column '$columnDisplayName' added to table '$tableName'."

3. Add a Yes/No (Boolean) Column

# Define column properties
$columnName = "IsActive"
$columnDisplayName = "Is Active"

# Create Boolean column
$column = New-Object Microsoft.Xrm.Sdk.Entity("BooleanAttributeMetadata")
$column["LogicalName"] = $columnName
$column["DisplayName"] = @{ "LocalizedLabels" = @( @{ "Label" = $columnDisplayName; "LanguageCode" = 1033 }) }
$column["DefaultValue"] = $true

# Add column
New-CdsTableColumn -Connection $connection -EntityLogicalName $tableName -Column $column
Write-Host "Boolean column '$columnDisplayName' added to table '$tableName'."

Step 5: Update an Existing Column in Dataverse

You can update properties of an existing column, such as the display name or required status.

# Define the column to update
$columnName = "EmailAddress"

# Retrieve the existing column
$column = Get-CdsTableColumn -Connection $connection -EntityLogicalName $tableName | Where-Object { $_.LogicalName -eq $columnName }

# Modify column properties
$column.DisplayName = @{ "LocalizedLabels" = @( @{ "Label" = "Customer Email"; "LanguageCode" = 1033 }) }
$column.IsRequired = $true # Make the field mandatory

# Update column
Set-CdsTableColumn -Connection $connection -EntityLogicalName $tableName -Column $column
Write-Host "Column '$columnName' updated successfully."

Step 6: Delete a Column from a Dataverse Table

You can remove an existing column using the Remove-CdsTableColumn cmdlet.

# Define the column to delete
$columnName = "CustomerAge"

# Delete the column
Remove-CdsTableColumn -Connection $connection -EntityLogicalName $tableName -ColumnLogicalName $columnName -Confirm:$false
Write-Host "Column '$columnName' deleted from table '$tableName'."

Step 7: Publish Customizations

After making changes (adding, updating, or deleting columns), publish the customizations to apply them.

Publish-CdsCustomization -Connection $connection
Write-Host "Customizations published successfully."

Step 8: Verify the Changes

1. List Columns Again to Verify Updates

# Retrieve updated columns for the table
$columns = Get-CdsTableColumn -Connection $connection -EntityLogicalName $tableName

# Display updated columns
$columns | Select-Object LogicalName, DisplayName, AttributeType

Step 9: Disconnect from Dataverse

After completing your task, disconnect the session.

Disconnect-CdsService -Connection $connection
Write-Host "Disconnected from Dataverse."

Leave a Reply

Your email address will not be published. Required fields are marked *