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");

Initialize

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');

Add(templateObj)

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

Arguments

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

The sample code below adds a template to your account:

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

var status = Template.Add(myTemp);

Retrieve(options)

This function retrieves a template based on the specified criteria.

Arguments

  • 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);

Update(attributes)

This function updates a template and returns a status.

Arguments

  • 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 11:33, 9 Apr 2014