Creating a New Table in Dataverse using PowerShell

Loading

Microsoft Dataverse is a scalable data platform that allows structured data storage for Power Platform applications. Using PowerShell, you can create new tables (entities) in Dataverse to store and manage business data efficiently.

This guide provides a step-by-step process to create a new table in Dataverse 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

You must authenticate and establish a connection with 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: Create a New Table in Dataverse

To create a table, you need to define:

  • Logical Name (internal name without spaces, e.g., “new_customerdata”)
  • Display Name (friendly name, e.g., “Customer Data”)
  • Primary Column (main identifier, e.g., “Customer Name”)

1. Define Table Schema and Create Table

# Define new table properties
$tableName = "new_CustomerData"
$tableDisplayName = "Customer Data"
$primaryColumnName = "CustomerName"
$primaryColumnDisplayName = "Customer Name"

# Create table
$entity = New-Object Microsoft.Xrm.Sdk.Entity("EntityDefinition")
$entity["LogicalName"] = $tableName
$entity["DisplayName"] = @{ "LocalizedLabels" = @( @{ "Label" = $tableDisplayName; "LanguageCode" = 1033 }) }
$entity["Description"] = @{ "LocalizedLabels" = @( @{ "Label" = "Stores customer information"; "LanguageCode" = 1033 }) }
$entity["PrimaryNameAttribute"] = $primaryColumnName

# Execute the request
$tableResponse = New-CdsTable -Connection $connection -Entity $entity
Write-Host "Table '$tableDisplayName' created successfully with logical name '$tableName'."

Step 4: Add Columns to the Table

1. Add a Text Column (e.g., Email Address)

# Define new 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 '$tableDisplayName'."

2. Add a Number Column (e.g., Customer Age)

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

# Create 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 '$tableDisplayName'."

Step 5: Publish Customizations

After creating the table and columns, publish the changes to apply them.

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

Step 6: Verify the Created Table

1. List All Tables to Verify

# Get all tables and check if the new table exists
$tables = Get-CdsTable -Connection $connection
$tables | Where-Object { $_.LogicalName -eq "new_CustomerData" } | Select-Object LogicalName, DisplayName

2. List Columns in the Table

# Get table columns
$columns = Get-CdsTableColumn -Connection $connection -EntityLogicalName "new_CustomerData"
$columns | Select-Object LogicalName, DisplayName

Step 7: Export Table Structure to CSV

If you want to document your Dataverse schema, you can export the table structure.

# Export table structure
$tables | Export-Csv -Path "C:\Dataverse_Tables.csv" -NoTypeInformation
Write-Host "Dataverse tables exported to C:\Dataverse_Tables.csv"

Step 8: Disconnect from Dataverse

After completing your tasks, 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 *