Dynamics 365 – Share Records using C# | GrantAccessRequest

Advertisements
Advertisements

It is necessary to specify the access rights to be granted to the recipient when sharing a record with another user using the GrantAccess message. If you need to modify the access of a shared record, the ModifyAccess message should be used.

For a shared record, access rights can vary for each user with whom the record is shared. It is important to note that a user cannot be given any rights that they do not already have for that particular type of table, based on the role assigned to them. For instance, if a user lacks the Read privileges for accounts and an account is shared with that user, they will be unable to view said account.

Share records by using C#

The ShareRecord static method provides an example of using the PrincipalAccess Class to designate a reference to a principal (user, team, or organization). This reference includes a set of AccessRights containing the rights to be granted to the principal.

/// <summary>
/// Shares a record with a principal
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="principal">The user, team, or organization to share the record with.</param>
/// <param name="access">The access rights to grant</param>
/// <param name="record">The record to share</param>
static void ShareRecord(
IOrganizationService service,
EntityReference principal,
AccessRights access,
EntityReference record)
{
PrincipalAccess principalAccess = new()
{
AccessMask = access,
Principal = principal
};
GrantAccessRequest request = new()
{
PrincipalAccess = principalAccess,
Target = record
};
service.Execute(request);
}
view raw ShareRecord.cs hosted with ❤ by GitHub

Modify Access by using C#

The following examples demonstrate how to use the ModifyAccess message to alter the access permissions granted to a principal for a shared record.

/// <summary>
/// Modifies the access to a shared record.
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="principal">The user, team, or organization to modify rights to the shared.</param>
/// <param name="access">The access rights to modify</param>
/// <param name="record">The shared record</param>
static void ModifyShare(
IOrganizationService service,
EntityReference principal,
AccessRights access,
EntityReference record)
{
PrincipalAccess principalAccess = new()
{
AccessMask = access,
Principal = principal
};
ModifyAccessRequest request = new()
{
PrincipalAccess = principalAccess,
Target = record
};
service.Execute(request);
}
view raw ModifyShare.cs hosted with ❤ by GitHub

Revoke Access by using C#

The owner of the record has the ability to use the RevokeAccess message in order to revoke (remove) a user’s access to the shared record.

The following example demonstrates how to remove sharing access for a user to a record using the RevokeAccessRequest Class through the RevokeShare static method.

/// <summary>
/// Revokes access to a shared record.
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="principal">The user, team, or organization to revoke rights to the shared record.</param>
/// <param name="record">The shared record</param>
static void RevokeShare(
IOrganizationService service,
EntityReference principal,
EntityReference record)
{
RevokeAccessRequest request = new()
{
Revokee = principal,
Target = record
};
service.Execute(request);
}
view raw RevokeAccess.cs hosted with ❤ by GitHub
Advertisements
Advertisements
Advertisements

Leave a comment

Create a website or blog at WordPress.com