Share Entity Records in Microsoft Dynamics CRM 2011

Posted: October 24, 2012 in Code Snippets, CRM 2011

Share Records to User

For Sharing a record in CRM 2011 you can use GrantAccess, ModifyAccess and RevokeAccess Messages. In my example im gonna share account entity record to systemuser.

public static void ShareObject(EntityReference systemUser, EntityReference account, IOrganizationService service)
        {
            // Create the PrincipalAccess Object
            PrincipalAccess principalAccess = new PrincipalAccess
            {
                // Gives the principal read write access
                AccessMask = AccessRights.ReadAccess | AccessRights.WriteAccess,

                // Set the PrincipalAccess Object's Properties
                Principal = systemUser
            };

            // Create the Request Object
            GrantAccessRequest grantAcessRequest = new GrantAccessRequest();

            // Set the Request Object's properties
            grantAcessRequest.PrincipalAccess = principalAccess;

            // Set the Target. In my case it is account record
            grantAcessRequest.Target = account;

            // Execute the Request
            service.Execute(grantAcessRequest);
        }

To Modify the shared record permissions you can use “ModifyAccess” Message

public static void ModifyObject(EntityReference systemUser, EntityReference account, IOrganizationService service)
        {
            // Create the PrincipalAccess Object
            PrincipalAccess principalAccess = new PrincipalAccess
            {
                // Gives the Full access on the record
                AccessMask = (AccessRights)852023,

                // Set the PrincipalAccess Object's Properties
                Principal = systemUser
            };

            // Create the Request Object
            ModifyAccessRequest modifyAcessRequest = new ModifyAccessRequest();

            // Set the Request Object's properties
            modifyAcessRequest.PrincipalAccess = principalAccess;

            // Set the Target. In my case it is account record
            modifyAcessRequest.Target = account;

            // Execute the Request
            service.Execute(modifyAcessRequest);
        }

To Revoke the permissions to the shared record you can use “RevokeAccess” Message

public static void RevokeObject(EntityReference systemUser, EntityReference account, IOrganizationService service)
        {
            // Create the Request Object
            RevokeAccessRequest revokeAcessRequest = new RevokeAccessRequest();

            // Set the Request Object's properties
            revokeAcessRequest.Revokee = systemUser;

            // Set the Target. In my case it is account record
            revokeAcessRequest.Target = account;

            // Execute the Request
            service.Execute(revokeAcessRequest);
        }

Share Records to Team

In the same way if you want to share or revoke permission to team, You can pass Team EntityReference instead of SystemUser.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s