Recently, I was working on a plugin where I was suppose to assign Roles to Users based on the business requirement.
Fetching the userid and roleid was not a tough task.
** Make sure you have the roleid related to the BusinessUnit to which the User belongs, otherwise an error will be thrown while associating the roles to that specific user.
The Main code for associating the specified Roles to the respective Users are:
AssociateRequest ar = new AssociateRequest(); ar.RelatedEntities = new EntityReferenceCollection(); ar.RelatedEntities.Add(new EntityReference("systemuser", newuserid)); ar.Relationship = new Relationship("systemuserroles_association"); ar.Target = new EntityReference("role", roleid); service.Execute(ar);
Hope that was helpful.