locked
Writing a plugin when reply back to an email and attach a logo RRS feed

  • Question

  • Hello,

    I've got some emails in  a QueueItem. When the user replies back to the email, he wants to be able to attach a logo to the footer of the email? I want to develop this functionality with a plugin. However, I'm not sure what type of message this plugin should be registered on and how the plugin should be written? Can you please help?


    Friday, March 31, 2017 6:45 PM

All replies

  • Use email signature.

    It will come under Options 


    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Monday, April 3, 2017 11:33 AM
  • Put the logo in web resource and use this method in you plugin:-

     private string GetUpdatedEmailBody(string originalEmailBody, string recordUrl, string webResourcePath)
            {
                //string internalLogoUrl = GetInternalLogoUrl(recordUrl,webResourcePath);  // this line was used in case we wanted to embed an imaged stored in CRM as Web Resource
                string internalLogoUrl = recordUrl;
                //string findText = "<img src=\"http://crm13s5:5555/Trade/WebResources/new_logo.png\">";
                string findText = "{Logo_DO_NOT_REMOVE}";
                //string replaceText = "<img width=\"175\" height=\"92\" src=\"" + internalLogoUrl + "\"/>";
                string replaceText = "<img src=\"" + internalLogoUrl + "\"/>";
    
                string updatedEmailBody = originalEmailBody.Replace(findText, replaceText);
                return updatedEmailBody;
            }
    

    Update and then send email

                Entity email = service.Retrieve(Email.LogicalEntityName, emailReference.Id,new ColumnSet(true));
                string body = email.Attributes[Email.Fields.Description].ToString();
                string updatedBody = GetUpdatedEmailBody(body, ApplicationRecordUrl.Get(executionContext), LogoRelativePath.Get(executionContext));
    
                Entity updatedEmail = new Entity() { Id = email.Id, LogicalName = Email.LogicalEntityName };            
                updatedEmail.Attributes[Email.Fields.Description] = updatedBody;
                service.Update(updatedEmail);
                tracingService.Trace("Email record updated with the logo");
                
                // Send Email
                SendEmailRequest sendEmailreq = new SendEmailRequest
                {
                    EmailId = email.Id,
                    TrackingToken = "",
                    IssueSend = true
                };
    
                SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

    :-


    Regards Faisal

    Monday, April 3, 2017 4:21 PM
  • Hi Faisal, Thanks for your reply. How should I use that web resource in the plugin? Also, on what message the plugin should be registered on? Thanks, NAR
    Monday, April 3, 2017 5:34 PM
  • You just need to copy paste above code after create of email and use it. The message will be create of email. First you will create an email and put {Logo_DO_NOT_REMOVE} text in the email where you want logo to appear. This email will be update with logo and sent by above code.

    Regards Faisal


    Tuesday, April 4, 2017 8:38 AM
  • I see. Thanks. What is the ApplicationRecordUrl in your code? How can I access that? Isn't that the email id?

    Thanks,

    Nar

    Tuesday, April 4, 2017 6:46 PM
  • I don't need to send the email. I just need to update the email body with a logo when they click reply. I have my code below but on the line where it updates the email, it complains that the email Id doesn't exist. Can you please let me know what I'm doing wrong?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Discovery;
    using Microsoft.Xrm.Sdk.Messages;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk.Client;
    namespace Plugin
    {
        public class AttachLogoToQueueItem : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                Entity entity = null;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName != "email") { return; }
                }
                try
                {
                    if (context.MessageName == "Create")
                    {
                        var emailId = entity.Id;
                        string body = entity.Attributes["description"].ToString();
                        string LogoRelativePath = "<img src=\"http://Logo\" > ";
                        string updatedBody = "<html><body>" + body + "<br/><br/>" + LogoRelativePath + "</body></html>";
                        //string updatedBody = "<html><body>" + LogoRelativePath + "</body></html>";
                        Entity updatedEmail = new Entity("email");
                        updatedEmail.Attributes["activityid"] = emailId;
                        updatedEmail.Attributes["description"] = updatedBody;
                        service.Update(updatedEmail);
                                          
                    }
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
                }
                catch (Exception ex)
                {
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                    throw;
                }
            }
           
        }
    }

    • Edited by Nar_Na Wednesday, April 5, 2017 5:14 PM
    Wednesday, April 5, 2017 5:12 PM
  • I figured it out. The reason I get the email id does not exist is that I registered the plugin on Pre-operation. Therefore, I can update the body of the email with the target entity. I don't need to instantiate a new entity and update it:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Discovery;
    using Microsoft.Xrm.Sdk.Messages;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk.Client;
    namespace Plugin
    {
        public class AttachLogoToQueueItem : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                Entity entity = null;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName != "email") { return; }
                }
                try
                {
                    if (context.MessageName == "Create")
                    {
                        var emailId = entity.Id;
                        string body = entity.Attributes["description"].ToString();
                        string LogoRelativePath = "<img src=\"http://Logo\" > ";
                        string updatedBody = "<html><body>" + LogoRelativePath + "<br/><br/>" + body + "</body></html>";
                        entity.Attributes["description"] = updatedBody;
                       
                    }
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                    throw;
                }
            }

        }
    }

    Thanks for your help.

           

    • Edited by Nar_Na Wednesday, April 5, 2017 6:30 PM
    Wednesday, April 5, 2017 6:29 PM