How-To Series 11: How To Trigger a plugin on Sharing a Record and Send An EMail

Hi All,
Today I have seen one forum post asking for Sending an email on Sharing a record. So, thought of implementing the same and blog it over here.
When a record is shared, a “PrincipalObjectAccess” record will be created in DB. However, we can’t register a plugin on “Create” Message for “PrincipalObjectAccess” entity. Instead, we can register it on “GrantAccess” message which will be triggered on sharing the record. So, simply register your plugin as follows:

Following is the piece of plugin code to get the details of record being shared and the users/team for whom the record has been shared. Here I am trying to send an email to the user for whom the record has been shared.

// The InputParameters collection contains all the data passed in the message request.if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is EntityReference)

{// Entity Record which has been shared
EntityReference sharedRecord = ((EntityReference)context.InputParameters[“Target”]);
// System User for whom the record has been shared
EntityReference sysUser = ((PrincipalAccess)context.InputParameters[“PrincipalAccess”]).Principal;

// System User ID who has shared the record
Guid fromUserId = context.UserId;
// Send An EMail
SendEmail(service, sysUser, fromUserId, sharedRecord);
}

I have shared the entire plugin code over here: Here

PS: If we have shared the record to multiple users then this plugin will also be triggered multiple times. This means that, if you have shared a record to 5 users then an email will be sent to 5 users seperately not in a single shot.

Hope it helps…!!! 🙂

One thought on “How-To Series 11: How To Trigger a plugin on Sharing a Record and Send An EMail

Leave a Reply

Your email address will not be published.