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
Clone or download the PowerApps-Samples repository.
Locate the /dataverse/webapi/C#-NETx/FunctionsAndActions/ folder.
Open the
FunctionsAndActions.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/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:
- Send FormatAddress Function with parameters for an address in the United States. Receive FormatAddressResponse ComplexType with the formatted address.
- Do the same with parameters for an address in Japan.
Section 3: Unbound Functions: InitializeFrom
Operations:
- Create an account record to be the original record.
- 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.
- 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:
- Detect if organization has the
sample_IsSystemAdmin
custom API installed. - If not, install solution in
IsSystemAdminFunction_1_0_0_0_managed.zip
containing the custom API. - Retrieve 10 systemuser records.
- 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:
- Create an account record to share.
- Retrieve an enabled user other than the current user.
- Use the RetrievePrincipalAccess Function to determine if the other user has AccessRights
'DeleteAccess'
on the account record created. - If they don't have
DeleteAccess
, use GrantAccess Action to share this access to the other user. - Test the user's access again using
RetrievePrincipalAccess
to verify that they now haveDeleteAccess
.
Section 8: Bound Actions: AddPrivilegesRole
Operations:
- Create a role EntityType record associated with the caller's business unit.
- Retrieve the role record with expanded privilege EntityType records to show the privileges included by default.
- Retrieve information about the
prvCreateAccount
andprvReadAccount
privileges. - Use the retrieved information about those privileges to prepare a list of RolePrivilege ComplexType instances to be parameters for
AddPrivilegesRole
. - Send AddPrivilegesRole Action with the
RolePrivilege
parameters. - Retrieve the role record again with expanded privilege EntityType records to show the privileges now include the
prvCreateAccount
andprvReadAccount
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#)