Adding Signature to Emails in Dynamics CRM

By | July 27, 2015

Introduction

Signatures are very integral part of any emails that are exchanged in present times. Dynamics CRM however does not yet have an OOB way to configure a signature for a user so that when a new Email is created, the signature is automatically added to the email body.

There is always the Insert Template button that can be used to add a template. You could create templates of your signature. But this requires that the user click on the Insert Template to get the template added to the email body.

Here is an alternate way of doing the same without requiring the users to click on the Insert Template button.

Walkthrough

We would need to create the following two components

  • Global Email Template – This would be used to store the signature format.
  • Script Web Resource to read and to insert global email template to email body
  • Global email template:

Create a Global email template to store the signature format of CRM User. Since it’s a global template we can add user details in the body of template.

In this template we have added user’s Full Name and Position.

signature

Whenever we instantiate this template the Full Name and Positions will get populated from logged-in user.

  • Script Web Resource to read and to modify global email template:

Create Web Resource of type script, in code retrieve above template and instantiate it.

After reading the template, call the InstantiateTemplate method and get the description (Signature of user) and set it on the email description.

Call function on load of email entity that will do below task,

 Get logged-in User id:

var userId = Xrm.Page.context.getUserId();

Execute Soap InstantiateTemplateRequest:

var Soapreq = “<request i:type=’b:InstantiateTemplateRequest’ xmlns:a=’http://schemas.microsoft.com/xrm/2011/Contracts’ xmlns:b=’http://schemas.microsoft.com/crm/2011/Contracts’>” +

“<a:Parameters xmlns:c=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’>” +

“<a:KeyValuePairOfstringanyType>” +

“<c:key>TemplateId</c:key>” +

“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>” + TemplateID + “</c:value>” +

“</a:KeyValuePairOfstringanyType>” +

“<a:KeyValuePairOfstringanyType>” +

“<c:key>ObjectType</c:key>” +

“<c:value i:type=’d:string’ xmlns:d=’http://www.w3.org/2001/XMLSchema’>systemuser</c:value>” +

“</a:KeyValuePairOfstringanyType>” +

“<a:KeyValuePairOfstringanyType>” +

“<c:key>ObjectId</c:key>” +

“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>”+userId+”</c:value>” +

“</a:KeyValuePairOfstringanyType>” +

“</a:Parameters>” +

“<a:RequestId i:nil=’true’ />” +

“<a:RequestName>InstantiateTemplate</a:RequestName>” +

“</request>”;

var result = XrmServiceToolkit.Soap.Execute(Soapreq);

Get the description (content of email template) from result and set it as the description (body) of email:

We get the subject and description field in result when we execute InstantiateTemplateRequest request.

Get the description field where we get the signature of user and set it to ‘description’ field of email.

      Xrm.Page.getAttribute(‘description’).setValue(description);

This way when we open an email activity the signature will be added with logged in user name.

signature1

Conclusion:

Not a perfect but certainly a workaround to get the signature working with minimal code.

Stuck with migration to Dynamics CRM? Experts at Inogic are always happy to help you, get in touch with us on crm@inogic.com.

Keywords : Dynamics CRM, CRM 2015, CRM 2015, Adding Signature to Emails, CRM 2011, Microsoft Dynamics CRM

2 thoughts on “Adding Signature to Emails in Dynamics CRM

  1. bewithsreenu@gmail.com'sri

    Hello,

    Thanks for your post.

    Implemented the same in MS CRM 2013 and couldn’t get the results as expected.

    I couldn’t get any description or subject field for var ‘result’ and couldn’t set the value to Email description.

    Please help me answering the below 2 questions:

    1) How to get the description of the email template from the var ‘result’ and assign the same to description field of Email?
    2) If this script is triggered on OnLoad of the email form,
    – will it not effect the user adding the signature only if the user wants to open the form. The user should only see the signature only while replying to the email and append it to the existing description of the email.

    Please help in achieving the solution.

    Thanks.
    Sri

    1. inogic Post author

      Hi Sri,

      Here are our comments on your two questions,
      1) How to get the description of the email template from the var ‘result’ and assign the same to description field of Email?

      Parse this response to get the description from the global email template,

      var response = XrmServiceToolkit.Soap.Execute(Soapreq); //Soapreq is the instantiate email request

      if ($(response).find(“a\\:Results”).length != 0) {
      getResult = $(response).find(“a\\:Results”);
      } else {
      getResult = $(response).find(“Results”); //chrome could not load node properly
      }

      if ($(getResult).find(“a\\:KeyValuePairOfstringanyType”).length != 0) {
      getAttribute = $(getResult).find(“a\\:KeyValuePairOfstringanyType”).eq(0)[0];
      } else {
      getAttribute = $(getResult).find(“KeyValuePairOfstringanyType”).eq(0)[0]; //chrome could not load node properly
      }

      for (var i = 0; i <= getAttribute.childNodes.length; i++) {
      if ($($($(getAttribute).find(“key”))[i]).text() != ”) {
      getKey = $($($(getAttribute).find(“key”))[i]).text();
      }
      else {
      getKey = $($($(getAttribute).find(“c\\:key”))[i]).text();
      }

      if (getKey == “description”) {
      if ($($($(getAttribute).find(“value”))[i]).text() != ”) {
      description = $($($(getAttribute).find(“value”))[i]).text();
      }
      else {
      description = $($($(getAttribute).find(“c\\:value”))[i]).text();
      }
      signature = emailBody; //here we get the email body of the given email template
      }
      }

      return signature;

      2) If this script is triggered on OnLoad of the email form,
      – will it not effect the user adding the signature only if the user wants to open the form. The user should only see the signature only while replying to the email and append it to the existing description of the email.

      Yes, the scripts triggers on OnLoad of the email form. If you want add a signature only if user replying email then you should check the email body content, if email body content already exist then only append signature in existing body else do nothing.

      To check if email is already has content then use below code.

      var description = Xrm.Page.getAttribute(“description”).getValue() != null ? Xrm.Page.getAttribute(“description”).getValue() : “”;
      if (description != “”) {
      //add signature
      Xrm.Page.getAttribute(“description”).setValue(signature);
      }

      Thanks !
      Sam

Leave a Reply