Share record with User or Team

Sharing records its a great way to allow users to see data. Several ways exist to fulfill this requirement, some of them are the access teams and share buttons. However, there’s another very useful way to share records, which is sharing records programmatically in a plugin. To do this,  you can use the following method:

public void Share_Record(EntityReference User_Or_Team, EntityReference Target_Entity, IOrganizationService service)
{
    // Create a new Grant Access Request
    GrantAccessRequest GrantAccess_Request = new GrantAccessRequest();

    // Set: Target Entity
    GrantAccess_Request.Target = Target_Entity;

    // Crate new Principal Access object
    PrincipalAccess Principal_Access = new PrincipalAccess();

    // Set the User Or Team to which the record
    // will be shared to
    Principal_Access.Principal = User_Or_Team;

    // Set: Access Mask. Here we'll define the privileges
    // that the User or Team will have over the record
    Principal_Access.AccessMask = AccessRights.ReadAccess |AccessRights.AppendAccess | AccessRights.WriteAccess |AccessRights.AppendToAccess |   AccessRights.ShareAccess |AccessRights.AssignAccess;

    // Set: Principal Access to Grant Access Request
    GrantAccess_Request.PrincipalAccess = Principal_Access;

    // Execute request
    GrantAccessResponse GrantAccess_Response = (GrantAccessResponse)service.Execute(GrantAccess_Request);
}

Hope it helps!

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 )

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