Tuesday 12 April 2016

C# Code for Removing Security Role to a User - CRM 2011/2013/2015/2016

Hi,

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.

public void RemoveUserSecurityRole(Guid guidSystemUserId, Guid guidSecurityRoleId, IOrganizationService crmService)
{
         // 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
        crmService.Execute(objDisassociateRequest);
}

Hope this helps.

--
Happy CRM'ing
Gopinath

No comments:

Post a Comment