CRM 2011 Online, EMail upon sharing a record RRS feed

  • Question

  • We have a requirement to email a user upon a record being shared with them, most importantly the opportunity.Is this possible? I thought this might be doable from workflow but i cant see an option for this.



    Thursday, August 18, 2011 1:38 PM


All replies

  • You cannot trigger a workflow when a record is shared. But you can do so in a plugin. You need to register a step on the GrantAccess message of the opportunity entity.

    Then from the plugin you can send the email.

    Gonzalo | gonzaloruizcrm.blogspot.com

    Thursday, August 18, 2011 1:55 PM
  • There is no option in workflow to trigger an email on the share event. A Plugin can be developed to do that which requires development.

    MS CRM MVP :: uMar Khan :: Microsoft CRM Consultant (Blog :: http://umarkhan.wordpress.com)
    Thursday, August 18, 2011 2:06 PM
  • Hi,

    You can find the complete solution via plugin on the following post: http://worldofdynamics.blogspot.com/2011/05/scenario-on-record-sharing-sending.html

    Jehanzeb Javeed

    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Jehanzeb.Javeed Thursday, August 18, 2011 2:11 PM
    • Marked as answer by Pete.CRM Thursday, August 18, 2011 2:14 PM
    Thursday, August 18, 2011 2:11 PM
  •  Thanks Jehanzeb, I'll give that a try.

    Thursday, August 18, 2011 2:13 PM
  • Hi,

    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.
     make sure in plugin registration tool you set it to pre validation.
    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";
                EntityCollection collToParty = new EntityCollection();
                collToParty.EntityName = "systemuser";
                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.
    • Proposed as answer by Sushant Sarkar Monday, April 30, 2012 10:01 AM
    Monday, April 30, 2012 10:01 AM
  • does this send an email automatically or do we have to trigger a workflow again?
    Tuesday, July 24, 2012 4:55 AM
  • It sends an email every time you share a lead no need to trigger any workflow

    Sushant Sarkar Microsoft CRM Dynamics Engineer

    Thursday, July 26, 2012 8:51 AM
  • Hi,

    In CRM workflows can not be triggered automatically on record sharing the above post mention an alliterative way to send trigger workflow on record sharing by using Plugins and Workflow. For more details you should refer this post: http://worldofdynamics.blogspot.com/2011/05/scenario-on-record-sharing-sending.html

    Jehanzeb Javeed

    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Thursday, July 26, 2012 8:57 AM
  • Dear Jehanzeb

    I had tried your above code but it fails to capture the shared user , i am using the on premise version of CRM 2011.  Is the above link only for crm 2011 online?



    Thursday, July 26, 2012 11:30 AM
  • Hi

    i was trying the same code. but could not generate email message?

    any help you can extend?

    Saturday, July 28, 2012 4:21 AM