Add Signature of current logged In User in MSCRM email using Javascript at new and reply of Email
function AddSignatureToEmail() { // Checks the form type and it should be 'create' type // Pre-fill a template signature var type = Xrm.Page.ui.getFormType(); if (type == "2") { var drawSignature = false; var emailTemplateToLoad = "C2A50A55-A481-E611-80C8-000C29EF4DA3"; //created email template Id var Id = Xrm.Page.data.entity.getId(); // Check if description is blank or similar //var theDescription = Xrm.Page.getAttribute("description").getValue(); var theDescription; var contactFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + " <entity name='email'>" + " <attribute name='description' />" + " <order attribute='subject' descending='false' />" + " <filter type='and'>" + " <condition attribute='activityid' operator='eq' value='" + Id + "'/>" + " </filter>" + " </entity>" + "</fetch>"; var contactRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML); if (contactRecords.length > 0) { if (contactRecords[0].attributes.description != undefined) theDescription = contactRecords[0].attributes.description.value; var theDescriptionOriginal = theDescription; } if (theDescription == null || theDescription != null) { drawSignature = true; } else if (theDescription == undefined) { drawSignature = true; } else if (stripHTML(theDescription) == "") { drawSignature = true; } else if (stripHTML(theDescription).length < 10) { drawSignature = true; } if (drawSignature) { // Get Regarding object details var regardingobjectid = Xrm.Page.getAttribute("regardingobjectid"); if (regardingobjectid == undefined) { return; } var RegardingItems = Xrm.Page.getAttribute("regardingobjectid").getValue(); // The signature will not append if regarding field remains blank. if (RegardingItems != null) { var regardingObjectId = RegardingItems[0].id; var regardingObjectType = RegardingItems[0].type; if (regardingObjectId == null || regardingObjectId == "") { return; } if (regardingObjectType == null || regardingObjectType == "") { return; } // Retrieving email template details var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate"); command.SetParameter("templateId", emailTemplateToLoad); command.SetParameter("objectId", regardingObjectId); command.SetParameter("objectTypeCode", regardingObjectType); var result = command.Execute(); if (result.Success) { var o = new Object(); o.EmailBody = "theDescription"; o.EmailSubject = ""; if (typeof (result.ReturnValue) == "string") { // Create a Xml Document of the return value to retrieve actual data oXml = CreateXmlDocument(result.ReturnValue); o.EmailBody = oXml.lastChild.lastElementChild.textContent; o.EmailBody += theDescriptionOriginal; Xrm.Page.getAttribute("description").setValue(o.EmailBody); } } } } } function stripHTML(signature) { var tmp = document.createElement("DIV"); tmp.innerHTML = signature; return tmp.textContent || tmp.innerText; } function CreateXmlDocument(signatureXmlStr) { // Function to create Xml formate of return email template data var parseXml; if (window.DOMParser) { parseXml = function (xmlStr) { return (new window.DOMParser()).parseFromString(xmlStr, "text/xml"); }; } else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) { parseXml = function (xmlStr) { var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(xmlStr); return xmlDoc; }; } else { parseXml = function () { return null; } } var xml = parseXml(signatureXmlStr); if (xml) { return xml; } } } var a = window.setTimeout(AddSignatureToEmail, 1500);