In any organization using Microsoft Dynamics 365 CRM, it’s important to manage who has access to different parts of the system. Security roles help control this access, making sure that each team member only sees what they need to. However, giving these roles to users one by one can take a lot of time, especially if you have to assign roles to many people at once. Luckily, Dynamics 365 CRM has tools that let you assign roles to multiple users all at once, whether by using the interface, Power Automate, or writing some code.
In this article, we’ll show you how to assign roles to multiple users in Dynamics 365 CRM, step by step.
Method 1: Using the Dynamics 365 CRM User Interface (UI)
The simplest way to assign roles to multiple users at once is through the Dynamics 365 CRM interface. Follow these steps:
- Navigate to Users Section:
- Go to Settings > Security > Users. Here, you’ll see a list of active users.
- Go to Settings > Security > Users. Here, you’ll see a list of active users.
- Select Multiple Users:
- Use the checkboxes next to each user’s name to select multiple users at once. If you want to assign roles to many users, you can use the filtering options to narrow down your list.
- Use the checkboxes next to each user’s name to select multiple users at once. If you want to assign roles to many users, you can use the filtering options to narrow down your list.
- Manage Roles:
- Once you’ve selected your users, click on the Manage Roles button in the command bar.
- Once you’ve selected your users, click on the Manage Roles button in the command bar.
- Assign Security Roles:
- A dialog box will appear showing all the available security roles. Select the role(s) you want to assign to the selected users by checking the appropriate boxes.
- A dialog box will appear showing all the available security roles. Select the role(s) you want to assign to the selected users by checking the appropriate boxes.
- Apply the Changes:
- Click OK to assign the chosen roles to all selected users. You’ll receive a confirmation message once the roles are successfully assigned.
Example: Let’s assume you want to assign the “Salesperson” role to 10 new users in your organization. You can filter the list by their job title or team, select all relevant users, click Manage Roles, check the “Salesperson” role, and click OK. All 10 users now have the “Salesperson” role assigned to them.
Method 2: Automating Role Assignment Using Power Automate
If you need to assign roles to multiple users regularly or based on specific conditions (e.g., when new users are created), Power Automate (formerly known as Microsoft Flow) offers a powerful way to automate this task.
Here’s how you can set up an automated flow:
- Create a Flow:
- In Power Automate, start by creating a new flow. You can set a trigger for this flow, such as When a record is created in the Common Data Service (now Dataverse), which will trigger the flow whenever a new user is added.
- In Power Automate, start by creating a new flow. You can set a trigger for this flow, such as When a record is created in the Common Data Service (now Dataverse), which will trigger the flow whenever a new user is added.
- List the Users:
- Use the List records action in the Common Data Service connector to retrieve a list of users. You can filter the list based on conditions, like department or job title, to ensure you’re assigning roles to the right group.
- Use the List records action in the Common Data Service connector to retrieve a list of users. You can filter the list based on conditions, like department or job title, to ensure you’re assigning roles to the right group.
- Assign Roles:
- Loop through the list of users with the Apply to Each action. Within this loop, use the Assign a security role action to assign the role(s) to each user. You’ll need to provide the security role ID and the user’s ID.
- Loop through the list of users with the Apply to Each action. Within this loop, use the Assign a security role action to assign the role(s) to each user. You’ll need to provide the security role ID and the user’s ID.
- Save and Test the Flow:
- Save your flow and test it to ensure that it correctly assigns roles to the users when the trigger condition is met.
Example: You can set up a flow to automatically assign the “Customer Service Representative” role to any new user added to the Customer Service department. The flow will run every time a new user record is created and check if the user is in the Customer Service department. If so, it will assign the appropriate role.
Method 3: Programmatically Assigning Roles Using a Plugin in Microsoft Dynamics 365 CRM
In addition to using Power Automate or the UI, you can also automate the assignment of security roles to multiple users using a custom plugin in Dynamics 365 CRM. This approach is particularly useful when you need to enforce role assignments based on specific business logic that runs whenever certain events occur in the CRM system.
Here’s how to create a plugin that assigns roles to multiple users.
Step 1: Create the Plugin
To begin, you’ll need to create a C# class library project in Visual Studio. The plugin will subscribe to an event, such as the creation of a user, and automatically assign a security role to that user.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
public class AssignRolePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Check if the plugin is triggered on a 'Create' message of a system user.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity userEntity = (Entity)context.InputParameters["Target"];
if (userEntity.LogicalName != "systemuser")
return;
// Define the Role ID you want to assign (replace with actual Role ID).
Guid roleId = new Guid("YOUR_ROLE_GUID_HERE");
// Associate the role with the newly created user.
service.Associate(
"systemuser",
userEntity.Id,
new Relationship("systemuserroles_association"),
new EntityReferenceCollection { new EntityReference("role", roleId) }
);
}
}
}
Step 2: Register the Plugin
After writing your plugin, you need to register it in Dynamics 365 CRM using the Plugin Registration Tool.
- Open Plugin Registration Tool:
- Connect to your CRM instance using the Plugin Registration Tool.
- Connect to your CRM instance using the Plugin Registration Tool.
- Register the New Assembly:
- Register the assembly containing your plugin.
- Register the assembly containing your plugin.
- Register the Plugin Step:
- Register a new step for the plugin on the
Create
message of thesystemuser
entity. - Set the Execution Mode to
Asynchronous
if you want the role assignment to happen in the background orSynchronous
for immediate execution.
- Register a new step for the plugin on the
- Deploy the Plugin:
- Once registered, deploy the plugin to your CRM instance.
Step 3: Test the Plugin
To test the plugin, create a new user in your CRM system. Upon creation, the plugin will automatically assign the specified role to the new user.
Example: Suppose you have a role called “Standard User” with the GUID 12345678-90ab-cdef-1234-567890abcdef
. You want every new user created in the CRM system to be automatically assigned this role. With the above plugin code, as soon as a new user is added, the plugin will associate the “Standard User” role with that user.
Advantages of Using a Plugin
- Automation: The plugin runs automatically whenever a new user is created, ensuring consistent role assignment.
- Flexibility: You can customize the plugin to include more complex logic, such as assigning different roles based on user attributes.
- Integration: Plugins integrate seamlessly with other CRM processes, making them a powerful tool for enforcing business rules.
Assigning roles to multiple users at once in Dynamics 365 CRM can be done through several approaches, depending on your needs:
- UI Method: Best for quick, manual assignments.
- Power Automate: Ideal for automating recurring tasks and integrating with workflows.
- C# Code: Useful for more complex scenarios and integrations.
Choosing the right method depends on the size of your organization and the frequency with which you need to assign roles to users. With these techniques, you can ensure that your users have the correct permissions quickly and efficiently.