Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This PowerShell sample demonstrates how to perform operations that create and modify table, column, and relationship definitions using the Dataverse Web API.
This sample implements the Dataverse operations and console output detailed in Web API table schema operations sample and uses the Dataverse Web API PowerShell Helper functions to manage authentication and provide reusable functions to perform common operations. These scripts are referenced using dot sourcing with the following lines:
. $PSScriptRoot\..\Core.ps1
. $PSScriptRoot\..\TableOperations.ps1
. $PSScriptRoot\..\CommonFunctions.ps1
. $PSScriptRoot\..\MetadataOperations.ps1
Note
This sample should work with Windows, Linux, and macOS, but has only been tested on Windows.
Prerequisites
Before running this sample, you should read these articles that explain concepts and patterns used by these samples:
- Quick Start Web API with PowerShell and Visual Studio Code
- Use PowerShell and Visual Studio Code with the Dataverse Web API
These articles have the same prerequisites.
Install or verify that the following are installed
Install Visual Studio Code. See Download Visual Studio Code
Install the PowerShell extension for Visual Studio Code. See PowerShell for Visual Studio Code
Install PowerShell 7.4 or higher. See Install PowerShell on Windows, Linux, and macOS
Install the Az PowerShell module version 11.1.0 or higher. See How to install Azure PowerShell
To update an existing installation to the latest version, use
Update-Module -Name Az -Force
Verify installation
Open Visual Studio Code.
In the Terminal menu, select New Terminal.
In Visual Studio Code navigation pane, select the
icon for the PowerShell extension.Copy and paste the following script in the Visual Studio Code terminal window:
Write-Host 'PowerShell Version:'$PSVersionTable.PSVersion.ToString() Write-Host 'PowerShell Az version:'(Get-InstalledModule Az).VersionPress Enter. The output should resemble the following:
PowerShell Version: 7.4.0 PowerShell Az version: 11.1.0
If you don't see results like this, install or update the prerequisites.
You'll also need
- A valid user account for a Dataverse environment
- The Url to the Dataverse environment you want to connect to. See View developer resources to learn how to find it. It looks something like this:
https://yourorg.crm.dynamics.com/, whereyourorg.crmis different. - Basic understanding of the PowerShell scripting language
How to run this sample
Clone or download the PowerApps-Samples repository.
Open the
/dataverse/webapi/PS/MetadataOperations/MetadataOperationsSample.ps1file using Visual Studio CodeEdit this line to use the URL of the environment you want to connect to:
Connect 'https://yourorg.crm.dynamics.com/' # change this(Optional) Set the
$deleteCreatedRecordsvariable to$falseif you don't want to delete the records this sample creates.Press F5 to run the sample.
The first time you run the sample a browser window opens. In the browser window, enter or select the credentials you want to use to authenticate.
To connect as a different user, run the Disconnect-AzAccount command and try again.
Code
The code for this sample is at: PowerApps-Samples/dataverse/webapi/PS/MetadataOperations/MetadataOperationsSample.ps1
Demonstrates
This sample has 11 regions:
Section 0: Create Publisher and Solution
Operations: Create a solution record and an associated publisher record.
Note
All solution components created in this sample will be associated to the solution so that they can be exported. For operations without a named message, this association is created using the MSCRM.SolutionUniqueName request header setting the solution unique name set as the value.
All names of solution components are prefixed using the publisher customization prefix.
Section 1: Create, Retrieve and Update Table
Operations:
- Create a new
sample_BankAccountuser-owned table by sending aPOSTrequest to/EntityDefinitions. - Retrieve the created table by sending a
GETrequest to/EntityDefinitions(LogicalName='sample_bankaccount'). - Update the table by sending a
PUTrequest to/EntityDefinitions(LogicalName='sample_bankaccount').
Section 2: Create, Retrieve and Update Columns
Operations:
- Attempt to retrieve a
sample_booleanboolean column by sending aGETrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes(LogicalName='sample_boolean'). - If the column doesn't already exist, create a new
sample_booleanboolean column for thesample_BankAccounttable by sending aPOSTrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes. - Update the
sample_booleanboolean column by sending aPUTrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes(LogicalName='sample_boolean'). - Update the option labels for the
sample_booleanboolean column using the UpdateOptionValue action. - Attempt to retrieve a
sample_datetimedatetime column for thesample_BankAccounttable, and create it if it doesn't exist. - Attempt to retrieve a
sample_decimaldecimal column for thesample_BankAccounttable, and create it if it doesn't exist. - Attempt to retrieve a
sample_integerinteger column for thesample_BankAccounttable, and create it if it doesn't exist. - Attempt to retrieve a
sample_memomemo column for thesample_BankAccounttable, and create it if it doesn't exist. - Attempt to retrieve a
sample_moneymoney column for thesample_BankAccounttable, and create it if it doesn't exist. - Attempt to retrieve a
sample_choicechoice column for thesample_BankAccounttable, and create it if it doesn't exist. - Add a new option to the
sample_choicecolumn using the InsertOptionValue action. - Change the order of the options of the
sample_choicecolumn using the OrderOption action. - Delete one of the options of the
sample_choicecolumn using the DeleteOptionValue action. - Attempt to retrieve a
sample_multiselectchoicemulti-select choice column for thesample_BankAccounttable, and create it if it doesn't exist. - Create a new Status option for the
sample_BankAccounttable using the InsertStatusValue Action.
Section 3: Create and use Global OptionSet
Operations:
- Create a new global choice named
sample_colorsby sending aPOSTrequest to/GlobalOptionSetDefinitions. - Retrieve the
sample_colorsglobal choice by sending aGETrequest to/GlobalOptionSetDefinitions(<id value>). - Create a new
sample_colorschoice column for thesample_BankAccounttable using thesample_colorsglobal choice by sending aPOSTrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributesand associating it to the global choice.
Section 4: Create Customer Relationship
Operations:
- Create a new
sample_customeridcustomer column for thesample_BankAccounttable using the CreateCustomerRelationships Action. - Retrieve the
sample_customeridcustomer column by sending aGETrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes(LogicalName='sample_customerid'). - Retrieve the relationships created for the customer column by sending
GETrequests to/RelationshipDefinitions(<id>)/Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata.`.
Section 5: Create and retrieve a one-to-many relationship
Operations:
- Verify that the
sample_BankAccounttable is eligible to be referenced in a 1:N relationship using the CanBeReferenced function. - Verify that the
contacttable is eligible to be reference other tables in a 1:N relationship using the CanBeReferencing function. - Identify what other tables can reference the
sample_BankAccounttable in a 1:N relationship using the GetValidReferencingEntities function. - Create a 1:N relationship between
sample_BankAccountandcontacttables by sending aPOSTrequest to/RelationshipDefinitions. - Retrieve the 1:N relationship by sending
GETrequest to/RelationshipDefinitions(<id>)/Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata.
Section 6: Create and retrieve a many-to-one relationship
Operations:
- Create a N:1 relationship between
sample_BankAccountandaccounttables by sendingPOSTa request to/RelationshipDefinitions. - Retrieve the N:1 relationship by sending a
GETrequest to/RelationshipDefinitions(<id>)/Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata.
Section 7: Create and retrieve a many-to-many relationship
Operations:
- Verify that the
sample_BankAccountandcontacttables are eligible to participate in a N:N relationship using the CanManyToMany function. - Verify that the
sample_BankAccountandcontacttables are eligible to participate in a N:N relationship using the GetValidManyToMany Function. - Create a N:N relationship between
sample_BankAccountandcontacttables by sending aPOSTrequest to/RelationshipDefinitions. - Retrieve the N:N relationship by sending
GETrequest to/RelationshipDefinitions(<id>)/Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata.
Section 8: Export managed solution
Operations: Export the solution created in Section 0: Create Publisher and Solution containing the items created in this sample using the ExportSolution Action.
Section 9: Delete sample records
Operations: A reference to each record created in this sample was added to a list as it was created. In this sample the records are deleted in the reverse order in which they were created.
Section 10: Import and Delete managed solution
Operations:
- Import the solution exported in Section 8 using the ImportSolution action.
- Query the solution table to get the
solutionidof the imported solution. - Delete the imported solution using the
solutionid.
Clean up
By default this sample will delete all the records created in it. If you want to view created records after the sample is completed, change the deleteCreatedRecords variable to false and you will be prompted to decide if you want to delete the records.
See also
Use the Dataverse Web API
Use the Web API with table definitions
Web API Samples
Web API Basic Operations Sample (PowerShell)