Showing results for 
Search instead for 
Did you mean: 
Super User
Super User

Code Review - Performance Impact - Assign security roles to users in bulk

Hello Experts,


I use below code to assign security role to a single user.


public static void AssignSecurityRole(Guid guidSystemUserId, Guid guidSecurityRoleId, IOrganizationService crmService)
            // Create new Associate Request object for creating a N:N relationsip between User and Security
            AssociateRequest objAssociateRequest = new AssociateRequest();
            // Create related entity reference object for associating relationship
            // In this case we SystemUser entity reference  
            objAssociateRequest.RelatedEntities = new EntityReferenceCollection();
            objAssociateRequest.RelatedEntities.Add(new EntityReference("systemuser", guidSystemUserId));
            // Create new Relationship object for System User & Security Role entity schema and assigning it 
            // to request relationship property
            objAssociateRequest.Relationship = new Relationship("systemuserroles_association");
            // Create target entity reference object for associating relationship
            objAssociateRequest.Target = new EntityReference("role", guidSecurityRoleId);
            // Passing AssosiateRequest object to Crm Service Execute method for assigning Security Role to User



I am executing above code from my console utility. I see performance degrade when I need to assign multiple security roles to 10k users.


Need you help and guidance to improve my performance of the code.






Helpful resources

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Users online (1,888)