Edit

Share via


Sample: Associate security role to a user

Learn how to associate a system user with a security role by using the Associate message or method.

Related articles:

About the sample code

Sample Description Build target
AssociateSecurityRoleToUser Demonstrates associating a user with a role. .NET 9

The code sample demonstrates how to associate a system user with a security role. Specifically, the samples demonstrate how to:

  1. Connect to Dataverse using a connection string that defines required connection information
  2. Query for a security role using its name attribute.
  3. Associate the logged on user with that security role.

Additional information can be found in README-code-design file.

How to build and run the code sample

  1. Clone the PowerApps-Samples repository.
  2. Locate the sample folder.
  3. Open the solution file (*.sln) in Visual Studio.
  4. Edit the project's appsettings.json file and set the Urlvalue as appropriate for your Dataverse test environment.
  5. Build and run the project [F5].
  6. You'll be prompted in a browser window for account sign-in credentials to the target environment.

Expected program output

For a successful run, the program's console output should look similar to the following example. Otherwise, any errors or exceptions are displayed.

Discovering who you are...done.
Associating your system user record with role 'Basic User'..done.

Use the Power Platform admin center to see that you now have
the 'Basic User' role. Afterwards, remove the role if desired.
Press any key to undo environment data changes.