Sharing Record in CRM using C# Code


Here is the code to share a record with a Team or User via C# Code. I have not given Delete Access, as I don’t want to give in my requirement. You can check the same and remove/add access accordingly.


/// Share the Record with the Team or User

/// CRM Organization Service
/// Target Entity Reference
/// Team or User Entity Reference
public void ShareRecord(IOrganizationService service, EntityReference erfTargetEntity, EntityReference erfTeamOrUser)
    // no delete access
    GrantAccessRequest objGrantAccessRequest = new GrantAccessRequest();
    objGrantAccessRequest.Target = erfTargetEntity;
    PrincipalAccess principal = newPrincipalAccess();
    principal.Principal = erfTeamOrUser;
    principal.AccessMask = AccessRights.ReadAccess | AccessRights.AppendAccess | AccessRights.WriteAccess | AccessRights.AppendToAccess | AccessRights.ShareAccess | AccessRights.AssignAccess;
    objGrantAccessRequest.PrincipalAccess = principal;
    GrantAccessResponse grantAccessResponse = (GrantAccessResponse)service.Execute(objGrantAccessRequest);

Refer this link for Un sharing a record.

Hope this helps.

Happy CRM’ing

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s