Pages

Saturday, 6 July 2013

CRM 2011: Share/Unshare records with a Team or a User using CRM SDK, C#


Sharing and Unsharing records with a Team using Microsoft Dynamics CRM 2011 SDK (early bound classes). You can also do a slight modification to use this code for sharing records with specific users.

Share Record with Team - Read only Access

public static void TeamShareWithReadAccess(IOrganizationService service, Guid recordId, string recordEntityName, Guid teamId)
        {
            GrantAccessRequest grantAccessRequest = new GrantAccessRequest()
            {
                Target = new EntityReference(recordEntityName, recordId),

                PrincipalAccess = new PrincipalAccess()
                {
                    Principal = new EntityReference(Team.EntityLogicalName, teamId),
                    AccessMask = AccessRights.ReadAccess,
                }
            };

            GrantAccessResponse grantAccessResponse = (GrantAccessResponse)service.Execute(grantAccessRequest);
        }

Share Record with Team - Read-Write Access (+Share, Assign, Append and AppendTo access)

public static void TeamShareWithReadWriteAccess(IOrganizationService service, Guid recordId, string recordEntityName, Guid teamId)
        {
            GrantAccessRequest grantAccessRequest = new GrantAccessRequest()
            {
                Target = new EntityReference(recordEntityName, recordId),

                PrincipalAccess = new PrincipalAccess()
                {
                    Principal = new EntityReference(Team.EntityLogicalName, teamId),
                    AccessMask = AccessRights.ReadAccess | AccessRights.WriteAccess | AccessRights.ShareAccess | AccessRights.AssignAccess | AccessRights.AppendAccess | AccessRights.AppendToAccess,
                }
            };

            GrantAccessResponse grantAccessResponse = (GrantAccessResponse)service.Execute(grantAccessRequest);
        }

Revoke Shared Access from Team

 public static void RevokeTeamShare(IOrganizationService service, Guid recordId, string recordEntityName, Guid teamId)
        {
            RevokeAccessRequest revokeAccessRequest = new RevokeAccessRequest()
            {
                Target = new EntityReference(recordEntityName, recordId),
                Revokee = new EntityReference(Team.EntityLogicalName, teamId)
            };

            RevokeAccessResponse revokeAccessResponse = (RevokeAccessResponse)service.Execute(revokeAccessRequest);
        }

No comments:

Post a Comment