Friday, 13 April 2012

Programmatically Sharing a Record in CRM 2011

In this article, I am defining how to share a record programmatically
To share a record we need to use these namespaces
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
Code to share a record with specific user :
GrantAccessRequest grantRequest = new GrantAccessRequest()
{
   Target = new EntityReference(Account.EntityLogicalName, _accountId),
   PrincipalAccess = new PrincipalAccess()
   {
      Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
      AccessMask = AccessRights.WriteAccess | AccessRights.ReadAccess | AccessRights.ShareAccess
   }
};

// Execute the request.
GrantAccessResponse grantResponse = (GrantAccessResponse)_service.Execute(grantRequest);
Code to remove share access from specific user :
ModifyAccessRequest modifyRequest = new ModifyAccessRequest()
{
  Target = new EntityReference(Account.EntityLogicalName, _accountId),
  PrincipalAccess = new PrincipalAccess()
  {
   Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
   AccessMask = AccessRights.None
  }
};

// Execute the request.
ModifyAccessResponse modifyResponse = (ModifyAccessResponse)_service.Execute(modifyRequest);
Code to modify share access for specific user :
ModifyAccessRequest modifyRequest = new ModifyAccessRequest()
{
  Target = new EntityReference(Account.EntityLogicalName, _accountId),
  PrincipalAccess = new PrincipalAccess()
  {
   Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
   AccessMask = AccessRights.ReadAccess | AccessRights.ShareAccess
  }
};

// Execute the request.
ModifyAccessResponse modifyResponse = (ModifyAccessResponse)_service.Execute(modifyRequest);
Code to retrieve user share access for specific record :
RetrievePrincipalAccessRequest retrieveRequest = new RetrievePrincipalAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId),
    Principal = new EntityReference(SystemUser.EntityLogicalName, _userId)
};

// Execute the request.
RetrievePrincipalAccessResponse retrieveResponse = (RetrievePrincipalAccessResponse)_service.Execute(retrieveRequest);
Code to retrieve all users share access from specific record :
RetrieveSharedPrincipalsAndAccessRequest retrieveSharedRequest = new RetrieveSharedPrincipalsAndAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId)
};

// Execute the request.
RetrieveSharedPrincipalsAndAccessResponse retrieveSharedResponse = (RetrieveSharedPrincipalsAndAccessResponse)_service.Execute(retrieveSharedRequest);
Code to revoke the share access for specific record :
RevokeAccessRequest revokeRequest = new RevokeAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId),
    Revokee = new EntityReference(SystemUser.EntityLogicalName, _userId)
};

// Execute the request.
RevokeAccessResponse revokeResponse = (RevokeAccessResponse)_service.Execute(revokeRequest);

No comments:

Post a Comment