none
CRM 2011 Plugins : Send E-mail using Plugins

    Question

  • Dear All ,

    I am writing a plug in code on which i have to send a email , I was not able to add the xrmServiceContext and Activitypart for "from" and "to" inside my plugin code eventhough i have added most references

     

     

    Thanks

    raga

    Thursday, January 05, 2012 10:33 AM

Answers

  • Hi

    to use the xrmServiceContext  you need to use crmsvcutil to generate the early bind classes. After that you can see the reference to ActivityParty too.

    But you can't assign the Activityparty to From or To fields as they take the array of ActivityParties not just one activity party.

    Here is a sample from sdk

    // Create the 'From:' activity party for the email
                ActivityParty fromParty = new ActivityParty
                {
                    PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
                };

                // Create the 'To:' activity party for the email
                ActivityParty toParty = new ActivityParty
                {
                    PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
                };
                Console.WriteLine("Created activity parties.");

                // Create an e-mail message.
                Email email = new Email
                {
                    To = new ActivityParty[] { toParty },
                    From = new ActivityParty[] { fromParty },
                    Subject = "SDK Sample e-mail",
                    Description = "SDK Sample for SendEmail Message.",
                    DirectionCode = true
                };
                _emailId = _serviceProxy.Create(email);     


    // Use the SendEmail message to send an e-mail message.
          SendEmailRequest sendEmailreq = new SendEmailRequest
          {
             EmailId = _emailId,
             TrackingToken = "",
             IssueSend = true
         };

         SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);

    I hope this helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    Thursday, January 05, 2012 12:20 PM
  • Hi,

    The QualifyLead step doesn't contain a property with name target - that's why your plugin doesn't do anything.

    You can find the input parameters of the context on this link: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.qualifyleadrequest_members.aspx

    You have to call something like this:

    EntityReference qualifiedLead = (EntityReference)context.InputParameters["LeadId"];

    Greets,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Tuesday, February 21, 2012 10:23 AM
  • Tuesday, February 21, 2012 4:09 PM
    Moderator

All replies

  • There is a sendemailrequest on the SOAP service you could use but there is no way to send the email that I am aware of via a context (using LINQ).
    Jamie Miley
    Check out my about.me profile!
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Thursday, January 05, 2012 12:20 PM
    Moderator
  • Hi

    to use the xrmServiceContext  you need to use crmsvcutil to generate the early bind classes. After that you can see the reference to ActivityParty too.

    But you can't assign the Activityparty to From or To fields as they take the array of ActivityParties not just one activity party.

    Here is a sample from sdk

    // Create the 'From:' activity party for the email
                ActivityParty fromParty = new ActivityParty
                {
                    PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
                };

                // Create the 'To:' activity party for the email
                ActivityParty toParty = new ActivityParty
                {
                    PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
                };
                Console.WriteLine("Created activity parties.");

                // Create an e-mail message.
                Email email = new Email
                {
                    To = new ActivityParty[] { toParty },
                    From = new ActivityParty[] { fromParty },
                    Subject = "SDK Sample e-mail",
                    Description = "SDK Sample for SendEmail Message.",
                    DirectionCode = true
                };
                _emailId = _serviceProxy.Create(email);     


    // Use the SendEmail message to send an e-mail message.
          SendEmailRequest sendEmailreq = new SendEmailRequest
          {
             EmailId = _emailId,
             TrackingToken = "",
             IssueSend = true
         };

         SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);

    I hope this helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    Thursday, January 05, 2012 12:20 PM
  • hi i've used same as said above and registered the plugin in qualify of lead->postevent->synchronous->server

    my code is:

     using System;

    using System.Diagnostics;

    using System.Linq;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Crm.Sdk.Messages;

    public class Plugin : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            Entity entity;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName == "lead")

                {

                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    var id = (Guid)context.OutputParameters["id"];

                    Lead ld = new Lead();

                    try

                    {

                        Guid l = (Guid)context.OutputParameters["id"];

                      

                            ActivityParty ac = new ActivityParty

                            {

                                PartyId = new EntityReference(SystemUser.EntityLogicalName, context.UserId)

                            };

                            ActivityParty ab = new ActivityParty

                            {

                                PartyId = new EntityReference(Lead.EntityLogicalName, l)

                            };

                            Console.WriteLine("Created activity parties");

                            Email em = new Email

                            {

                               

                                From=new ActivityParty[]{ac},

                                To = new ActivityParty[] { ab },

                                Subject = "Regarding the winning",

                                Description = "The lead has been qualified",

                                RegardingObjectId = (EntityReference)context.OutputParameters["id"],

                                DirectionCode = true

                            };

                            service.Create(em);

                            Guid _emailId = service.Create(em);

                            SendEmailRequest sendEmailreq = new SendEmailRequest

                            {

                                EmailId = _emailId,

                                TrackingToken = "",

                                IssueSend = true

                            };

                            SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

                                               

                    }

                    catch (FaultException<OrganizationServiceFault> ex)

                    {

                        throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in. "+ ex.Message);

                    }

                    catch (Exception ex)

                    {

                        throw new InvalidPluginExecutionException(ex.Message);

                    }

                   

                }

            }

        }

    }

    plugin is not getting triggered i think and no email activity is created.

    Tuesday, February 21, 2012 7:10 AM
  • Hi,

    The easiest way to check if a plugin is triggered is to throw a new InvalidPluginExecutionException on top of the Execute-method - so get the entered message as popup when the plugin is triggered...

    Also check the input parameter - the input parameters are different per event... On which event have you registered the plugin?

    Greets,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Tuesday, February 21, 2012 9:46 AM
  • hi,

    I've registered the plugin in QualifyLead Step for lead. and want to creat an email activity. I'm a fresher to dynamics CRM and this is my first trial in early bound type.

    Tuesday, February 21, 2012 10:06 AM
  • Hi,

    The QualifyLead step doesn't contain a property with name target - that's why your plugin doesn't do anything.

    You can find the input parameters of the context on this link: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.qualifyleadrequest_members.aspx

    You have to call something like this:

    EntityReference qualifiedLead = (EntityReference)context.InputParameters["LeadId"];

    Greets,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Tuesday, February 21, 2012 10:23 AM
  • Tuesday, February 21, 2012 4:09 PM
    Moderator
  • thanks for the responses. The probelm is the Email is getting created but the From and to part is not getting filled (Activityparty). Is there any sample code in sdk 2011 for email activity.

    Wednesday, February 22, 2012 11:23 AM