Auf Englisch lesen

Teilen über

Web API PowerShell Metadata operations sample

This PowerShell sample demonstrates how to work with the following using the Dataverse Web API:

  • Table definitions
  • Column definitions
  • Option set definitions
  • Relationship definitions
  • Importing and exporting solutions

This sample uses the common helper libraries created as described in Use PowerShell and Visual Studio Code with the Dataverse Web API. These functions are defined in the MetadataOperations.ps1 file. You can find descriptions and examples in Dataverse Web API PowerShell Helper Metadata operations

Prerequisites

Before running this sample you should read these articles that explain concepts and patterns used by these samples:

This sample requires:

How to run the sample

  1. Clone or download the PowerApps-Samples repository.

  2. Open the MetadataOperationsSample.ps1 file using Visual Studio Code

  3. Edit this line to use the URL of the environment you want to connect to:

    Connect 'https://yourorg.crm.dynamics.com/' # change this

  4. (Optional) Set the $deleteCreatedRecords variable to $false if you do not want to delete the records this sample creates.

  5. Press F5 to run the sample.

  6. 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.

Demonstrates

This sample has 11 sections:

  1. Create Publisher and Solution
  2. Create, Retrieve and Update Table
  3. Create, Retrieve and Update Columns
  4. Create and use Global OptionSet
  5. Create Customer Relationship
  6. Create and retrieve a one-to-many relationship
  7. Create and retrieve a many-to-one relationship
  8. Create and retrieve a many-to-many relationship
  9. Export managed solution
  10. Delete sample records
  11. Import and Delete managed solution

Section 0: Create Publisher and Solution

Operations: Create a solution record and an associated publisher record.

  • After first checking whether these records already exist using the Get-Records function, the script creates them if they don't exist using the New-Record function. These functions are in the TableOperations.ps1 file.
  • All solution components created in this sample get associated to the solution, so they can be exported. 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.
  • Details about these records are added to the $recordsToDelete array so that they can be deleted later.

Section 1: Create, Retrieve and Update Table

Operations:

Section 2: Create, Retrieve and Update Columns

Operations:

  • Check whether a boolean column named sample_Boolean exists in the sample_BankAccount table using the Get-TableColumns function.
  • If the column doesn't exist, create a new boolean column for the sample_BankAccount table using the New-Column function.
  • Retrieve the boolean column using the Get-Column function.
  • Update the boolean column using the the Update-Column function.
  • Update the option labels for the boolean column using the Update-OptionValue function.
  • Query, create, and retrieve a new datetime column for the sample_BankAccount table using the Get-TableColumns, New-Column, and Get-Column functions.
  • Query, create, and retrieve a new decimal column for the sample_BankAccount table.
  • Query, create, and retrieve a new integer column for the sample_BankAccount table.
  • Query, create, and retrieve a new memo column for the sample_BankAccount table.
  • Query, create, and retrieve a new money column for the sample_BankAccount table.
  • Query, create, and retrieve a new choice (Picklist) column for the sample_BankAccount table.
  • Query, create, and retrieve a new choices (MultiSelectPicklist) column for the sample_BankAccount table.
  • Query, create, and retrieve a new big int column for the sample_BankAccount table.
  • Create a new Status option for the sample_BankAccount table using the New-StatusOption function.

Section 3: Create and use Global OptionSet

Operations:

Section 4: Create Customer Relationship

Operations:

  • Check whether a customer column exists using the Get-TableColumns function.
  • If it doesn't exist, create a new customer column for the sample_BankAccount table using the New-CustomerRelationship function.
  • Retrieve the customer column using the Get-Column function
  • Retrieve the relationships created for the customer column using the Get-Relationship function.

Section 5: Create and retrieve a one-to-many relationship

Operations:

Section 6: Create and retrieve a many-to-one relationship

Operations:

Section 7: Create and retrieve a many-to-many relationship

Operations:

Section 8: Export managed solution

Operations: Export the solution containing the items created in this sample using the Export-Solution function

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 they were created using the Remove-Record function

Section 10: Import and Delete managed solution

Operations:

  • Import the solution exported in Section 8 using the Import-Solution function
  • Query the solution table to get the id of the imported solution
  • Delete the imported solution.

Clean up

By default, this sample deletes all the records it created.

If you want to view created records, after the sample is completed, change the deleteCreatedRecords variable to false and you're prompted to delete the records if desired.

Hinweis

If you don't delete the unmanaged solution components created by this sample, the code in Section 10 fails.

Console Output

The complete output to the console should look something like this:

text
PS C:\GitHub\PowerApps-Samples\dataverse\webapi\PS>
PS C:\GitHub\PowerApps-Samples\dataverse\webapi\PS> . 'C:\GitHub\PowerApps-Samples\dataverse\webapi\PS\MetadataOperations\MetadataOperationsSample.ps1'
Example Publisher created successfully
Metadata Example Solution created successfully
Bank Account table created successfully
Retrieved Bank Account table.
Bank Account table updated successfully
Sample Boolean column created successfully
Original Option Labels:
 True Option Label: True
 False Option Label: False
Sample Boolean Column updated successfully
Option values updated successfully
Updated Option Labels:
 True Option Label: Up
 False Option Label: Down
Example DateTime column created successfully
Retrieved Sample DateTime column.
 DateTimeBehavior: DateOnly
 Format: DateOnly
Sample Decimal column created successfully
Retrieved Sample Decimal column.
 MaxValue: 100
 MinValue: 0
 Precision: 1
Sample Integer column created successfully
Retrieved Sample Integer column.
 MaxValue: 100
 MinValue: 0
 Format: None
Sample Memo column created successfully
Retrieved Sample Memo column.
 Format: Text
 ImeMode: Disabled
 MaxLength: 500
Sample Money column created successfully
Retrieved Sample Money column.
 MaxValue: 1000
 MinValue: 0
 Precision: 1
 PrecisionSource: 1
 ImeMode: Disabled
Sample Choice column created successfully
Retrieved Sample Choice column.
Retrieved Choice column options:
 Value: 727000000 Label: Bravo
 Value: 727000001 Label: Delta
 Value: 727000002 Label: Alpha
 Value: 727000003 Label: Charlie
 Value: 727000004 Label: Foxtrot
Echo option added to the local optionset.
Retrieved Sample Choice column again.
Retrieved Choice column options:
 Value: 727000000 Label: Bravo
 Value: 727000001 Label: Delta
 Value: 727000002 Label: Alpha
 Value: 727000003 Label: Charlie
 Value: 727000004 Label: Foxtrot
 Value: 727000005 Label: Echo
Choice column options re-ordered.
Retrieved Sample Choice column again.
Retrieved Choice column options with new order:
 Value: 727000002 Label: Alpha
 Value: 727000000 Label: Bravo
 Value: 727000003 Label: Charlie
 Value: 727000001 Label: Delta
 Value: 727000005 Label: Echo
 Value: 727000004 Label: Foxtrot
Foxtrot option deleted from the local optionset.
Sample MultiSelect Choice column created successfully
Retrieved Sample MultiSelect Choice column.
Retrieved MultiSelect Choice column options:
 Value: 727000000 Label: Appetizer
 Value: 727000001 Label: Entree
 Value: 727000002 Label: Dessert
Sample BigInt column created successfully
Retrieved Sample BigInt column.
 MaxValue: 9223372036854775807
 MinValue: -9223372036854775808
Frozen status added to the status column.
With the value of: 727000000
Retrieved Status Reason column again.
Retrieved status column options:
 Value: 1 Label: Active State: 0
 Value: 2 Label: Inactive State: 1
 Value: 727000000 Label: Frozen State: 1
Colors global optionset created successfully                                                                
Retrieved Colors global optionset.
Retrieved global optionset options:
 Value: 727000000 Label: Red
 Value: 727000001 Label: Yellow
 Value: 727000002 Label: Green
Example Colors Choice column created successfully
Retrieved Sample Colors Choice column.
Retrieved Choice column options:
 Value: 727000000 Label: Red
 Value: 727000001 Label: Yellow
 Value: 727000002 Label: Green
Customer relationship created successfully
Example Customer Lookup column created successfully
Retrieved Sample Bank Account owner column Targets:
 account
 contact
Retrieved Customer relationship IDs:
 sample_BankAccount_Customer_Account
 sample_BankAccount_Customer_Contact
The Bank Account table is eligible to be a primary table in a one-to-many relationship.
The Bank Account table is eligible to be a related table in a one-to-many relationship.
The contact table is in the list of potential referencing entities for the Bank Account table.
sample_BankAccount_Contacts One-to-Many relationship created successfully
sample_Account_BankAccounts Many-to-One relationship created successfully
The contact table can participate in many-to-many relationships.
The Bank Account table can participate in many-to-many relationships.
The contact table is in the list of tables that can participate in many-to-many relationships
The Bank Account table is in the list of tables that can participate in many-to-many relationships
sample_sample_BankAccounts_Contacts Many-to-Many relationship created successfully
Managed solution exported to C:\GitHub\PowerApps-Samples\dataverse\webapi\PS\MetadataOperations\metadataexamplesolution.zip
Deleting sample records...
RelationshipDefinitions record with ID: 7746b5ec-7c11-ef11-9f89-7c1e520b124e deleted.
RelationshipDefinitions record with ID: 037f08e2-7c11-ef11-9f89-6045bdec757e deleted.
RelationshipDefinitions record with ID: db9953d9-7c11-ef11-9f89-7c1e520b124e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: a49d9074-b9bc-425d-9ca6-8b846269316c deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 5302e1c4-7c11-ef11-9f89-6045bdec757e deleted.
GlobalOptionSetDefinitions record with ID: 7c2d43c3-7c11-ef11-9f89-7c1e520b124e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: cd48ebbf-7c11-ef11-9f89-6045bdec7f44 deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 201015b8-7c11-ef11-9f89-6045bdec757e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 496638ab-7c11-ef11-9f89-6045bdec757e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 873943a2-7c11-ef11-9f89-7c1e520b124e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 554ba99e-7c11-ef11-9f89-6045bdec7f44 deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 7479559c-7c11-ef11-9f89-7c1e5214ffc0 deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: 2ae45795-7c11-ef11-9f89-7c1e520b124e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: c2c38696-7c11-ef11-9f89-6045bdec757e deleted.
EntityDefinitions(LogicalName='sample_bankaccount')/Attributes record with ID: c5437c8b-7c11-ef11-9f89-6045bdec7f44 deleted.
EntityDefinitions record with ID: 441f9871-7c11-ef11-9f89-6045bdec7f44 deleted.
solutions record with ID: 411f9871-7c11-ef11-9f89-6045bdec7f44 deleted.
publishers record with ID: 3e1f9871-7c11-ef11-9f89-6045bdec7f44 deleted.
Importing managed solution...

Managed solution imported.
Managed solution deleted.
PS C:\GitHub\PowerApps-Samples\dataverse\webapi\PS>