Thursday, December 30, 2010

Assign roles to users using Code

There are times when we need to assign roles promatically may plugin or else threu any applications suing the sdk.
here is the code to assign the role.
make sure u have the suer GUID and the roles GUIDs u wan to assign.
and keep it in mind that the higher role previlege will apply for that user.

private static void assignRole(Guid userGUID, Guid[] rolesGUID)
{
// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "Default";

CrmService service = new CrmService();
service.Url = "http://<server>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the request object.
AssignUserRolesRoleRequest assign = new AssignUserRolesRoleRequest();

// Set the properties of the request object.
assign.UserId = userGUID;
// Set the ID of a role that is in the same business unit as the user.
assign.RoleIds = rolesGUID;

// Execute the request.
AssignUserRolesRoleResponse assigned = (AssignUserRolesRoleResponse)service.Execute(assign);
}

Sudhanshu

No comments:

Post a Comment