locked
To Send Email To Shared Users with Whom the Lead is Shared in MS CRM 2011 RRS feed

  • General discussion

  • Hi All,

    You can use this way to send an email to the users with whom the lead is shared by writing a plugin and trigerring it on GrantAccess.

                                                        
    namespace ShareMail
    {
        public class SendEmailOnAccessRights: IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {           

                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                 // Obtain the target entity from the input parmameters.                        
                string body = "The following lead is shared with you";
                string sub = "Lead Shared";  

                if ((context.MessageName != "GrantAccess" || context.MessageName != "RevokeAccess") && context.Stage != 10 && context.PrimaryEntityName != "lead")
                { return; }

                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                EntityReference entity = (EntityReference)context.InputParameters["Target"];
                string id = entity.Id.ToString();        

                if (context.InputParameters.Contains("PrincipalAccess"))
                {
                    PrincipalAccess principal = context.InputParameters["PrincipalAccess"] as PrincipalAccess;
                    EntityReference sharedUser = principal.Principal;
                    AccessRights accessRights = principal.AccessMask;
                }

                if (context.InputParameters.Contains("Revokee"))
                {
                    EntityReference revokedUser = context.InputParameters["Revokee"] as EntityReference;
                }

                SendEmail(service, (context.InputParameters["PrincipalAccess"] as PrincipalAccess).Principal.Id, context.UserId, new Guid(id), body, sub);         
            }

            private void SendEmail(IOrganizationService service, Guid recieverUserId, Guid senderUserId, Guid regardingObjectId, string emailBody, string emailSubject)
            {
                Entity email = new Entity();
                email.LogicalName = "email";

     //Set regarding object property (i.e. The entity record, which u want this email associated with)

                EntityReference regardingObject = new EntityReference("lead", regardingObjectId);
                email.Attributes.Add("regardingobjectid", regardingObject);
                //Defining Activity Parties (starts)
                EntityReference from = new EntityReference("systemuser", senderUserId);
                EntityReference to = new EntityReference("systemuser", recieverUserId);
                //Derive from party
                Entity fromParty = new Entity("activityparty");
                fromParty.Attributes.Add("partyid", from);
                //Derive to party
                Entity toParty = new Entity("activityparty");
                toParty.Attributes.Add("partyid", to);
                EntityCollection collFromParty = new EntityCollection();
                collFromParty.EntityName = "systemuser";
                collFromParty.Entities.Add(fromParty);
                EntityCollection collToParty = new EntityCollection();
                collToParty.EntityName = "systemuser";
                collToParty.Entities.Add(toParty);
                email.Attributes.Add("from", collFromParty);
                email.Attributes.Add("to", collToParty);
                //Defining Activity Parties (ends)
                //Set subject & body properties
                email.Attributes.Add("subject", emailSubject);
                email.Attributes.Add("description", emailBody);
                //Create email activity
                Guid emailID = service.Create(email);
                //Sending email
                SendEmailRequest reqSendEmail = new SendEmailRequest();
                reqSendEmail.EmailId = emailID;//ID of created mail
                reqSendEmail.TrackingToken = "";
                reqSendEmail.IssueSend = true;
                SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
            }
        }
    }

    The above code worked for me very well. Hope this will serve developers with similar requirement.
    Monday, April 30, 2012 9:49 AM

All replies