Tags

, , ,

Hello Everyone,

Today I had a requirement of Sharing the Account record with the Created By User (with only Read Permission) before it is being Assigned to any new User as Owner. This was done so that the Created By user at-least keep Track of his created Accounts even when his ownership is passed on to other user during Escalation Process.

In order to achieve the above functionality,  I decided to write a plugin. In dynamics CRM ‘GrantAccessRequest‘ class is used to share any record with a team or a User.

So in accordance with my current business scenario as discussed in the first paragraph; I used the following piece of code in my plugin: 

Guid AccCreatedById = ((EntityReference)account.Attributes["createdby"]).Id;
 var CreatedReference = new EntityReference("systemuser", AccCreatedById);
 var grantAccessRequest = new GrantAccessRequest
 {
 PrincipalAccess = new PrincipalAccess
 {
 AccessMask = AccessRights.ReadAccess,
 Principal = CreatedReference
 },
 Target = new EntityReference("account", account.Id)
 };
service.Execute(grantAccessRequest);

Target refers to the record being shared, 

Principal refers to the User/Team to share.

You can even assign multiple rights as:

AccessMask = AccessRights.CreateAccess | AccessRights.WriteAccess | AccessRights.DeleteAccess| AccessRights.ShareAccess |AccessRights.AssignAccess,

 

Similarly, to revoke the access from any team or a user ‘RevokeAccessRequest‘ is used.

var revokeAccessReq = new RevokeAccessRequest
{
    Revokee = new EntityReference("systemuser", "GUID"),
    Target = new EntityReference("account", account.Id)
};
service.Execute(revokeAccessReq);

This was a new class in the Plugin which I came across recently. Hope this will too help someone.

Thanks !!!

😀