Friday, 1 January 2016

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.

/// <summary>
/// Share the Record with the Team or User
/// </summary>
/// <param name="service">CRM Organization Service</param>
/// <param name="erfTargetEntity">Target Entity Reference</param>
/// <param name="erfTeamOrUser">Team or User Entity Reference</param>
public void ShareRecord(IOrganizationService service, EntityReference erfTargetEntity, EntityReference erfTeamOrUser)
    // no delete access
    GrantAccessRequest objGrantAccessRequest = new GrantAccessRequest();
    objGrantAccessRequest.Target = erfTargetEntity;
    PrincipalAccess principal = new PrincipalAccess();
    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

No comments:

Post a Comment