In Dynamics 365, impersonation allows actions to be performed on behalf of another user, enabling more flexible and secure workflows. For instance, when automating business processes like case assignments, it’s important to ensure that the actions are attributed to the appropriate user rather than a system account. Impersonation ensures the integrity of audit logs, preserves data accuracy, and maintains compliance with user roles and permissions.
In this blog, we’ll explore how impersonation works in Dynamics 365 and demonstrate how to implement it in a Windows-based tool designed for bulk case assignments. This ensures that each case is attributed to the correct manager, even though an admin is executing the tool.
When performing bulk operations, it’s critical to maintain accurate and secure user attribution. For example, in a scenario where managers are supposed to assign cases to their respective salespeople, running the tool from an admin account might compromise the integrity of the process.
To ensure a smooth implementation, let’s first cover the necessary prerequisites:
In many organizations, a Windows-based tool can be developed for administrators or managers to handle bulk case assignments automatically. This tool, designed using .NET, interacts with Dynamics 365 to assign cases, ensuring that the correct user attribution is maintained throughout the process. With this tool, managers no longer need to manually assign cases to their salespeople, as the system can handle the assignment on their behalf.
To impersonate the manager, you first need to retrieve the GUID of the manager for each salesperson. This can be done using a simple query that retrieves the “managerid” for the given “systemuser” (salesperson) record.
var query = new QueryExpression("systemuser") { ColumnSet = new ColumnSet("systemuserid", "managerid"), }; query.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, salespersonId); var salesperson = service.RetrieveMultiple(query).Entities.FirstOrDefault(); if (salesperson != null) { var managerId = salesperson.GetAttributeValue("managerid")?.Id; if (managerId != null) { salespersonToManagerMapping[salespersonId] = managerId.Value; } }
This code retrieves the manager’s GUID for the specified salesperson and stores it in a dictionary for later use.
Once you have the manager’s GUID, you can use the ServiceClient to impersonate the manager when assigning cases to the appropriate salesperson. The CallerId property is set to the manager’s GUID before performing the update, ensuring that the action appears to have been performed by the manager.
var serviceClient = new ServiceClient(connectionString); // Process each case and assign to the correct salesperson foreach (var caseId in caseIds) { var salespersonId = GetSalespersonIdForCase(caseId); // Retrieve salesperson for the case if (salespersonToManagerMapping.ContainsKey(salespersonId)) { serviceClient.CallerId = salespersonToManagerMapping[salespersonId]; } // Create the case update request Entity caseUpdate = new Entity("incident") { Id = caseId, ["ownerid"] = new EntityReference("systemuser", salespersonId) }; // Update the case in Dynamics 365 serviceClient.Update(caseUpdate); }
In this example, each case is updated to reflect the correct owner (salesperson), and the CallerId is set to the manager’s GUID, ensuring the case assignment is performed under the manager’s identity.
If you’re processing a large number of cases, you may want to optimize the operation using ExecuteMultipleRequest to reduce the number of individual service calls. This ensures more efficient bulk processing and helps improve performance when dealing with a high volume of records.
var executeMultipleRequest = new ExecuteMultipleRequest { Requests = new OrganizationRequestCollection() }; foreach (var caseId in caseIds) { var salespersonId = GetSalespersonIdForCase(caseId); // Retrieve salesperson if (salespersonToManagerMapping.ContainsKey(salespersonId)) { serviceClient.CallerId = salespersonToManagerMapping[salespersonId]; } Entity caseUpdate = new Entity("incident") { Id = caseId, ["ownerid"] = new EntityReference("systemuser", salespersonId) }; var updateRequest = new UpdateRequest { Target = caseUpdate }; executeMultipleRequest.Requests.Add(updateRequest); } // Execute the bulk update var response = serviceClient.Execute(executeMultipleRequest);
Using ExecuteMultipleRequest, you can batch your updates and process them in a single request, reducing overhead and improving performance.
Implementing impersonation for bulk operations brings several benefits:
With impersonation in Dynamics 365, businesses can automate tasks securely and efficiently. When assigning cases in bulk, impersonating the correct manager ensures that actions are logged and performed under the right context, preserving audit trail integrity and maintaining compliance with user roles and permissions.
By leveraging impersonation, businesses can streamline their operations while maintaining transparency, accountability, and security, ensuring that Dynamics 365 remains a trusted and reliable tool for managing data and processes.
The post Impersonating a User in Dynamics 365 Via ServiceClient Or CrmServiceClient first appeared on Microsoft Dynamics 365 CRM Tips and Tricks.
Original Post https://www.inogic.com/blog/2025/05/impersonating-a-user-in-dynamics-365-via-serviceclient-or-crmserviceclient/