Επεξεργασία

Κοινή χρήση μέσω


Web API Functions and Actions Sample (C#)

This .NET 6.0 sample demonstrates how to perform common data operations 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 Functions and Actions Sample and uses the common C# constructs described in Web API Samples (C#).

Prerequisites

  • Microsoft Visual Studio 2022.
  • Access to Dataverse with privileges to perform data operations.

How to run this sample

  1. Clone or download the PowerApps-Samples repository.

  2. Locate the /dataverse/webapi/C#-NETx/FunctionsAndActions/ folder.

  3. Open the FunctionsAndActions.sln file using Visual Studio 2022

  4. Edit 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.
  5. Save the appsettings.json file

  6. Press F5 to run the sample.

Code

The code for this sample is here: PowerApps-Samples/dataverse/webapi/C#-NETx/FunctionsAndActions/Program.cs

Demonstrates

This sample has nine regions:

Section 1: Unbound Functions: WhoAmI

Operation: Send WhoAmI Function and receive WhoAmIResponse ComplexType.

Section 2: Unbound Functions: FormatAddress

Operations:

  1. Send FormatAddress Function with parameters for an address in the United States. Receive FormatAddressResponse ComplexType with the formatted address.
  2. Do the same with parameters for an address in Japan.

Section 3: Unbound Functions: InitializeFrom

Operations:

  1. Create an account record to be the original record.
  2. Send InitializeFrom Function with parameters referencing the account record created. Receive a response with data to create a new account record with values from the original record.
  3. Create a new account record using the data from InitializeFromResponse so that the new record is associated with the original record and potentially containing data copied from the original record, depending on how the column mappings are configured for the organization.

Section 4: Unbound Functions: RetrieveCurrentOrganization

Operation: Send RetrieveCurrentOrganization Function and receive RetrieveCurrentOrganization ComplexType.

Section 5: Unbound Functions: RetrieveTotalRecordCount

Operations: Send RetrieveTotalRecordCount Function with parameters for account and contact tables and receive RetrieveTotalRecordCountResponse ComplexType containing the number of records in each table.

Section 6: Bound Functions: IsSystemAdmin

Operations:

  1. Detect if organization has the sample_IsSystemAdmin custom API installed.
  2. If not, install solution in IsSystemAdminFunction_1_0_0_0_managed.zip containing the custom API.
  3. Retrieve 10 systemuser records.
  4. Loop through the records using the sample_IsSystemAdmin function to detect which ones have the System Administrator security role.

Section 7: Unbound Actions: GrantAccess

Operations:

  1. Create an account record to share.
  2. Retrieve an enabled user other than the current user.
  3. Use the RetrievePrincipalAccess Function to determine if the other user has AccessRights'DeleteAccess' on the account record created.
  4. If they don't have DeleteAccess, use GrantAccess Action to share this access to the other user.
  5. Test the user's access again using RetrievePrincipalAccess to verify that they now have DeleteAccess.

Section 8: Bound Actions: AddPrivilegesRole

Operations:

  1. Create a role EntityType record associated with the caller's business unit.
  2. Retrieve the role record with expanded privilege EntityType records to show the privileges included by default.
  3. Retrieve information about the prvCreateAccount and prvReadAccount privileges.
  4. Use the retrieved information about those privileges to prepare a list of RolePrivilege ComplexType instances to be parameters for AddPrivilegesRole.
  5. Send AddPrivilegesRole Action with the RolePrivilege parameters.
  6. Retrieve the role record again with expanded privilege EntityType records to show the privileges now include the prvCreateAccount and prvReadAccount privileges.

Section 9: Delete sample records

Operations: A reference to each record created in this sample was added to a list as it was created. This section sends a $batch request to delete the record.

Clean up

By default this sample deletes 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'll be prompted to decide if you want to delete the records.

See also

Perform conditional operations using the Web API
Use the Dataverse Web API
WebAPIService class library (C#)
Web API Samples
Web API Basic Operations Sample (C#)
Web API Query Data sample (C#)
Web API Conditional Operations sample (C#)
Web API table schema operations sample (C#)
Web API WebApiService Parallel Operations Sample (C#)
Web API Parallel Operations with TPL Dataflow components Sample (C#)