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 .NET 6.0 sample demonstrates how to perform operations that create and modify table, column, and relationship definitions using the Dataverse Web API.
This sample uses the common helper code in the WebAPIService class library (C#).
Note
This sample implements the Dataverse operations and console output detailed in Web API table schema operations sample and uses the common C# constructs described in Web API Samples (C#).
Prerequisites
The following is required to build and run this sample:
- Microsoft Visual Studio 2022.
- Access to Dataverse with privileges to perform data operations.
How to run this sample
Clone or download the PowerApps-Samples repository.
Locate the /dataverse/webapi/C#-NETx/MetadataOperations/ folder.
Open the
MetadataOperations.slnfile using Visual Studio 2022Edit the
appsettings.jsonfile to set the following property values:Property Instructions UrlThe Url for your environment. Replace the placeholder https://yourorg.api.crm.dynamics.comvalue with the value for your environment. See View developer resources to find this.UserPrincipalNameReplace the placeholder you@yourorg.onmicrosoft.comvalue with the UPN value you use to access the environment.PasswordReplace the placeholder yourPasswordvalue with the password you use.Save the
appsettings.jsonfilePress F5 to run the sample.
Code
The code for this sample is here: PowerApps-Samples/dataverse/webapi/C#-NETx/MetadataOperations/Program.cs
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:
- Create a new
sample_booleanboolean column for thesample_BankAccounttable by sending aPOSTrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes. - Retrieve the
sample_booleanboolean column by sending aGETrequest to/EntityDefinitions(LogicalName='sample_bankaccount')/Attributes(LogicalName='sample_boolean'). - 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. - Create and retrieve a new
sample_datetimedatetime column for thesample_BankAccounttable. - Create and retrieve a new
sample_decimaldecimal column for thesample_BankAccounttable. - Create and retrieve a new
sample_integerinteger column for thesample_BankAccounttable. - Create and retrieve a new
sample_memomemo column for thesample_BankAccounttable. - Create and retrieve a new
sample_moneymoney column for thesample_BankAccounttable. - Create and retrieve a new
sample_choicechoice column for thesample_BankAccounttable. - 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. - Create and retrieve a new
sample_multiselectchoicemulti-select choice column for thesample_BankAccount table. - 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 using a $batch operation.
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
WebAPIService class library (C#)
Use the Web API with table definitions
Web API Samples
Web API Basic Operations Sample (C#)
Web API Query Data sample (C#)
Web API Conditional Operations sample (C#)
Web API Functions and Actions Sample (C#)
Web API WebApiService Parallel Operations Sample (C#)
Web API Parallel Operations with TPL Dataflow components Sample (C#)