Hi,
You can write a plugin for triggering mail on sharing a record,Please check the below code:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
EntityReference Staffing = ((EntityReference)context.InputParameters["Target"]);
var staffingrecord = service.Retrieve(Staffing.LogicalName, Staffing.Id, new ColumnSet(new[] { "fullname" }));
staffingname = Convert.ToString(staffingrecord["fullname"]);
// User or Team for whom the record has been shared
EntityReference sharedRecord = ((PrincipalAccess)context.InputParameters["PrincipalAccess"]).Principal;
// System User ID who has shared the record
Guid fromUserId = context.UserId;
// Send An EMail
EmailUser(service, sharedRecord, fromUserId, Staffing);
}
}
private void EmailUser(IOrganizationService service, EntityReference sysUser, Guid fromUserId, EntityReference regarding)
{
Entity email = new Entity("email");
email.Attributes.Add("subject", "New Staffing Record Has been Shared With You");
email.Attributes.Add("description", " " + "A Staffing Record has recieved for the LEAD:" + " " + staffingname + "Please view the record shared with you " + " ");
email.Attributes.Add("regardingobjectid", regarding);
EntityReference from = new EntityReference("systemuser", fromUserId);
EntityReference to = sysUser;
Entity fromParty = new Entity("activityparty");
fromParty.Attributes.Add("partyid", from);
Entity toParty = new Entity("activityparty");
toParty.Attributes.Add("partyid", to);
EntityCollection frmPartyCln = new EntityCollection();
frmPartyCln.EntityName = "systemuser";
frmPartyCln.Entities.Add(fromParty);
EntityCollection toPartyCln = new EntityCollection();
toPartyCln.EntityName = "systemuser";
toPartyCln.Entities.Add(toParty);
email.Attributes.Add("from", frmPartyCln);
email.Attributes.Add("to", toPartyCln);
//Create an EMail Record
Guid _emailId = service.Create(email);
// Use the SendEmail message to send an e-mail message.
SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
}
Trigger Plugin on:GrantAccess Message