Web API Query schema definitions and detect changes sample (C#)
This sample shows how to retrieve and detect changes in table definitions using the RetrieveMetadataChanges Action.
You can view the sample at PowerApps-Samples/dataverse/webapi/C#-NETCore/Schema/RetrieveMetadataChanges/
See these articles for explanation of functionality:
This sample uses the common helper code in the WebAPIService class library (C#).
Prerequisites
The following prerequisites are 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/RetrieveMetadataChanges/ folder.
Open the
RetrieveMetadataChanges.sln
file using Visual Studio 2022Edit the
appsettings.json
file to set the following property values:Property Instructions Url
The Url for your environment. Replace the placeholder https://yourorg.api.crm.dynamics.com
value with the value for your environment. See View developer resources to find the Url for your environment.UserPrincipalName
Replace the placeholder you@yourorg.onmicrosoft.com
value with the UPN value you use to access the environment.Password
Replace the placeholder yourPassword
value with the password you use.Save the
appsettings.json
filePress F5 to run the sample.
Code
The code for this sample is here: PowerApps-Samples/dataverse/webapi/C#-NETx/RetrieveMetadataChanges/Program.cs
Demonstrates
This sample shows how to retrieve schema definitions for a specific set of column definitions and save them (in memory) to represent a cache.
Then it creates a new column, retrieves the data for only that new column, which it adds to the cache.
Then it deletes the column, retrieves data about deleted items and uses it to remove the deleted column definition from the cache.
This sample has six sections:
Define query
Define a query using EntityQueryExpression that returns all the Picklist choice columns from the contact table.
Initialize cache
- Create an instance of RetrieveMetadataChanges with the
Query
parameter set to the query. - Send the request and get a RetrieveMetadataChangesResponse.
- Cache the
RetrieveMetadataChangesResponse.EntityMetadata
value. - Save the
RetrieveMetadataChangesResponse.ServerVersionStamp
value for use in the next request. - Write a list of all the current columns in the cache.
Add choice column
Create a new choice column by creating a new PicklistAttributeMetadata instance in the contact table.
Detect added column
- Create a new instance of RetrieveMetadataChanges with the
Query
parameter set to the original query. - Set the
RetrieveMetadataChangesRequest.ClientVersionStamp
with the value previously returned from the first request. - Send the request and get a RetrieveMetadataChangesResponse.
- Verify that only one new column definition was returned to represent the choice column that was created.
- Save the
RetrieveMetadataChangesResponse.ServerVersionStamp
value for use in the next request. - Add that choice column data to the cache.
Delete choice column
Delete the choice column created earlier.
Detect deleted column
- Create a new instance of RetrieveMetadataChanges with the
Query
parameter set to the original query. - Set the
RetrieveMetadataChangesRequest.ClientVersionStamp
with the value previously returned from the second request. - Set the
RetrieveMetadataChangesRequest.DeletedMetadataFilters
toDeletedMetadataFilters.Attribute
because we're looking for deleted column definitions. - Send the request and get a RetrieveMetadataChangesResponse.
- Find the ID of the deleted choice column in the
RetrieveMetadataChangesResponse.DeletedMetadata
, usingDeletedMetadataFilters.Attribute
as an index value for the collection. - Remove the column definition from the cache.
- Write a list of all the current columns in the cache.
Clean up
No clean-up is required because all data created by this sample was deleted.
See also
Query Schema Definitions
Cache Schema data
Use the Dataverse Web API
WebAPIService class library (C#)
Web API table schema operations sample (C#)