In the last post we have seen how to assign a security role to the user programmatically.
Here is the code for removing the security roles of the users.
// Create new Disassociate Request object for creating a N:N link between User and Security
DisassociateRequest objDisassociateRequest = new DisassociateRequest();
// Create related entity reference object for associating relationship
// we will pass System User record reference of user for which the role is required to be removed
objDisassociateRequest.RelatedEntities = new EntityReferenceCollection();
objDisassociateRequest.RelatedEntities.Add(new EntityReference(“systemuser”, guidSystemUserId));
// Create new Relationship object for System User & Security Role entity schema and assigning it
// to request relationship property
objDisassociateRequest.Relationship = new Relationship(“systemuserroles_association”);
// Create target entity reference object for associating relationship
objDisassociateRequest.Target = new EntityReference(“role”, guidSecurityRoleId);
// Passing DisassociateRequest object to Crm Service Execute method for removing Security Role to User
Hope this helps.