Generate the Word Document in JavaScript using Word Templates


Pre-Requisites:

  1. Add XRMServiceToolKit Library (Click Here)
  2. Add Process Helper Library (Click Here)

Follow the Steps below.

//Fetch XML for Getting the ID of Document Template

var templateName = “Testing Template”;

var fetchXml = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>” +

“<entity name=’documenttemplate’>” +

“<attribute name=’documenttype’ />” +

“<attribute name=’name’ />” +

“<attribute name=’status’ />” +

“<attribute name=’modifiedon’ />” +

“<attribute name=’modifiedby’ />” +

“<attribute name=’description’ />” +

“<order attribute=’documenttype’ descending=’false’ />” +

“<order attribute=’name’ descending=’false’ />” +

“<filter type=’and’>” +

“<condition attribute=’documenttype’ operator=’eq’ value=’2′ />” +

“<condition attribute=’status’ operator=’eq’ value=’0′ />” +

“<condition attribute=’name’ operator=’eq’ value='” + templateName + “‘ />” +

“</filter>” +

“</entity>” +

“</fetch>”;

//Fetch the document template

var docTemplateResults = XrmServiceToolkit.Soap.Fetch(fetchXml);

//If Template found

if (docTemplateResults.length > 0) {

debugger;

//Get the template Id

var templateId = docTemplateResults[0].attributes.documenttemplateid.value;

// Call an Action

Process.callAction(“SetWordTemplate”,

[{

key: “Target”,

type: Process.Type.EntityReference,

value: new Process.EntityReference(“account”, Xrm.Page.data.entity.getId())

},

{

key: “SelectedTemplate”,

type: Process.Type.EntityReference,

value: new Process.EntityReference(“documenttemplate”, templateId)

}],

function (params) {

debugger;

// Success

alert(“Success” + params);

},

function (e, t) {

debugger;

// Error

alert(e);

// Write the trace log to the dev console

if (window.console && console.error) {

console.error(e + “\n” + t);

}

});

}

 

Advertisements

About Kishore Dhanekula

MS Dynamics CRM and .NET Architect
This entry was posted in CRM, Dynamics 365, Java Script, JavaScript, Ms Dynamics CRM 2013, MS Dynamics CRM 2015, MS Dynamics CRM 2016 and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s