Home > CRM 2011 > Copy a security role programmatically – CRM 2011

Copy a security role programmatically – CRM 2011

In one of the requirement, we have to create a new security role by copying an existing role in a Plug-in.

The whole requirement break down to 3 steps

  1. Create a new security role
  2. Get all the existing role privileges by using “RetrieveRolePrivilegesRoleRequest
    1. RetrieveRolePrivilegesRoleResponse” contain, “RolePrivileges” property, with collection of privileges & access level
    2. Role Privilages

      Role Privileges

  3. Add the retrieved role privileges to the newly created role by using “AddPrivilegesRoleRequest

Below is the code for step 2 & 3

Guid existingRoleId = new Guid(“C85F0FFF-4C80-E211-A877-1CC1DE79B4CA”);

Guid newRoleId = new Guid(“B6690FFF-4C80-E211-A877-1CC1DE79B4CA”);

// Step 2

RetrieveRolePrivilegesRoleRequest getPrivilagesRequest = new RetrieveRolePrivilegesRoleRequest();

getPrivilagesRequest.RoleId = existingRoleId;

RetrieveRolePrivilegesRoleResponse privilagesResponse = (RetrieveRolePrivilegesRoleResponse)service.Execute(getPrivilagesRequest);

if (privilagesResponse != null && privilagesResponse.RolePrivileges != null){

// Step 3

AddPrivilegesRoleRequest addPrivilagesRequest = new AddPrivilegesRoleRequest();

addPrivilagesRequest.Privileges = privilagesResponse.RolePrivileges;

addPrivilagesRequest.RoleId = newRoleId;

AddPrivilegesRoleResponse addPrivilagesResponse = (AddPrivilegesRoleResponse)service.Execute(addPrivilagesRequest);



  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: