How to Use the Template Server-Side JavaScript Functions

In your server-side JavaScript code, first load the core library using the syntax below: 

Platform.Load("core", "1");


To interact with an template via server-side JavaScript, you must first initialize the object. The code below initializes a list with the external key of 'myTemplate'.

var t = Template.Init('myTemplate');


This function adds a new template to your account and returns the status of the operation.


  • templateObj - a JSON object representing the properties of the new Template

The sample code below adds a template to your account:

var myTemp = {    
    "TemplateName":"SSJS Test Template",
    "LayoutHTML":"this is some HTML"

var status = Template.Add(myTemp);


This function retrieves a template based on the specified criteria.


  • options - The criteria on which to search for the account. You can pass the criteria in one of two ways:
    • a filter expression
    • a JSON object containing both a filter and additional search parameters.

The sample code below retrieves a template based on the specified filter criteria:

var getTemplate = Template.Retrieve({Property:"CustomerKey",SimpleOperator:"equals",Value:"MyTemplate"});

The sample code below queries across all applicable accounts and retrieves a template based on the filter criteria:

var query = {};

query.Filter = '{Property:"CustomerKey",SimpleOperator:"equals",Value:"MyTemplate"}'; 
query.QueryAllAccounts = true;

var getTemplate = Template.Retrieve(query);


This function updates a template and returns a status.


  • attributes- attributes regarding the template to be changed.

The sample code below updates the Name attribute of the template to the specified value:

var myTemplate = Template.Init('myTemplateCK');
var status = temp.Update({ "TemplateName" : "Edited Template" });
Was This Page Helpful?
Last updated by ryan.williams at 09:08, 12 Mar 2015