Wednesday, October 12, 2011

Dynamics CRM 2011 : C# Code for Removing Assigned Security Role to a User


Security Roles are associated with Users via N:N relationship (relationship name: systemuserroles_association). So now in CRM 2011 we can use DisassociateRequest to remove programmatically user assigned security roles .

[C# Code]:
The c # code below can be used in to remove the User assigned Security Role.


    public void RemoveUserSecurityRole(Guid prmUserId, Guid prmSecurityRoleId, IOrganizationService prmCrmWebService)
    {
        // Create new Disassociate Request object for creating a N:N link between User and Security
        DisassociateRequest wod_DisassociateRequest = new DisassociateRequest ();

        // Create related entity reference object for associating relationship
        // we will pass (SystemUser) record reference of user for which the role is required to be removed
           
        wod_DisassociateRequest.RelatedEntities = new EntityReferenceCollection();
        wod_DisassociateRequest.RelatedEntities.Add(new EntityReference("systemuser", prmUserId));

        // Create new Relationship object for System User & Security Role entity schema and assigning it
        // to request relationship property
           
        wod_DisassociateRequest.Relationship = new Relationship("systemuserroles_association");

        // Create target entity reference object for associating relationship
        wod_DisassociateRequest.Target = new EntityReference("role", prmSecurityRoleId);

        // Passing DisassociateRequest object to Crm Service Execute method for removing Security Role to User
        prmCrmWebService.Execute(wod_DisassociateRequest);
    }


Saturday, October 8, 2011

Dynamics CRM 2011 : C# Code for Assigning Security Role to a User


In Dynamics CRM 4.0 AssignUserRolesRoleRequest method was available to assign Security Roles to User but in CRM 2011 this method has been removed and no longer any special method is required to assign the Security Role to users, Security Roles are associated with Users via N:N relationship (relationship name: systemuserroles_association). So now in CRM 2011 we can use AssosiateRequest to assign the security roles to the user programmatically.
[C# Code]:

The following C# code can be used to assign Security Role to a User.

    public void AssignSecurityRole(Guid prmUserId, Guid prmSecurityRoleId, IOrganizationService prmCrmWebService)
    {
        // Create new Associate Request object for creating a N:N link between User and Security
        AssociateRequest wod_AssosiateRequest = new AssociateRequest();

        // Create related entity reference object for associating relationship
        // In our case we will pass (SystemUser) record reference 
           
        wod_AssosiateRequest.RelatedEntities = new EntityReferenceCollection();
        wod_AssosiateRequest.RelatedEntities.Add(new EntityReference("systemuser", prmUserId));

        // Create new Relationship object for System User & Security Role entity schema and assigning it
        // to request relationship property
           
        wod_AssosiateRequest.Relationship = new Relationship("systemuserroles_association");

        // Create target entity reference object for associating relationship
        wod_AssosiateRequest.Target = new EntityReference("role", prmSecurityRoleId);

        // Passing AssosiateRequest object to Crm Service Execute method for assigning Security Role to User
        prmCrmWebService.Execute(wod_AssosiateRequest);
    }