Running a Content Creation Operation for Email By Data Record (Using JSON)
Problem
You want to run a content creation operation to create and send email content using a design template and an existing set of Data Records as inputs.
Solution
The solution is to make a series of requests using the following URIs and method types to submit, monitor progress and ultimately retrieve the result of the content creation operation. There is also the option of cancelling an operation during processing if required. These requests can be submitted via the Content Creation (Email) REST service:
Process Content Creation (By Data Record) (JSON) | /rest/serverengine/workflow/contentcreation/email/{templateId} | POST |
Get Progress of Operation | /rest/serverengine/workflow/contentcreation/email/getProgress/{operationId} | GET |
Get Result of Operation | /rest/serverengine/workflow/contentcreation/email/getResult/{operationId} | POST |
Cancel an Operation | /rest/serverengine/workflow/contentcreation/email/cancel/{operationId} | POST |
Example
HTML5
cce-process-by-dre-json.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Process Content Creation (By Data Record) (JSON) Example</title>
<script src="../../common/lib/js/jquery-3.4.1.min.js"></script>
<script src="../../common/js/common.js"></script>
<script src="js/cce-process-by-dre-json.js"></script>
<link rel="stylesheet" href="../../common/css/styles.css">
</head>
<body>
<h2>Content Creation (Email) Service - Process Content Creation (By Data Record) (JSON) Example</h2>
<form>
<fieldset>
<legend>Inputs</legend>
<div>
<label for="datarecords">Data Record ID(s):</label>
<input id="datarecords" type="text" placeholder="1234, 2345, 3456, ..." required>
</div>
<div>
<label for="designtemplate">Design Template ID/Name:</label>
<input id="designtemplate" type="text" placeholder="1234 or Filename" required>
</div>
</fieldset>
<fieldset>
<legend>Email Parameters</legend>
<div>
<label for="section">Section:</label>
<input id="section" type="text" placeholder="Section Name">
</div>
<div>
<label for="sender">From:</label>
<input id="sender" type="text" placeholder="mailbox@domain.com" required>
</div>
<div>
<label for="sendername">From Name:</label>
<input id="sendername" type="text" placeholder="From Name">
</div>
<div>
<label for="host">Host:</label>
<input id="host" type="text" placeholder="mail.domain.com:port" required>
</div>
<div>
<label for="usesender">Use From as To Email Address:</label>
<input id="usesender" type="checkbox" checked>
</div>
<div>
<label for="attachpdf">Attach PDF Page to Email:</label>
<input id="attachpdf" type="checkbox">
</div>
<div>
<label for="attachweb">Attach Web Page to Email:</label>
<input id="attachweb" type="checkbox">
</div>
</fieldset>
<fieldset>
<legend>Email Security</legend>
<div>
<label for="useauth">Use Authentication:</label>
<input id="useauth" type="checkbox" checked>
</div>
<div>
<label for="starttls">Start TLS:</label>
<input id="starttls" type="checkbox">
</div>
<div>
<label for="username">Username:</label>
<input id="username" type="text" placeholder="Username">
</div>
<div>
<label for="password">Password:</label>
<input id="password" type="password" placeholder="Password">
</div>
</fieldset>
<fieldset>
<legend>Progress & Actions</legend>
<div>
<progress value="0" max="100"></progress>
</div>
<div>
<input id="cancel" type="button" value="Cancel" disabled>
<input id="submit" type="submit" value="Submit">
</div>
</fieldset>
</form>
</body>
</html>
JavaScript/jQuery
cce-process-by-dre-json.js
/* Content Creation (Email) Service - Process Content Creation (By Data Record) (JSON) Example */
(function ($, c) {
"use strict";
$(function () {
c.setupExample();
var $useAuth = $("#useauth"),
$startTLS = $("#starttls"),
$username = $("#username"),
$password = $("#password"),
$submitButton = $("#submit"),
$cancelButton = $("#cancel"),
$progressBar = $("progress"),
operationId = null;
$cancelButton.on("click", function () {
if (operationId !== null) {
/* Cancel an Operation */
$.ajax({
type: "POST",
url: "/rest/serverengine/workflow/contentcreation/email/cancel/" + operationId
})
.done(function (response) {
c.displayInfo("Operation Cancelled!");
operationId = null;
setTimeout(function () {
$progressBar.attr("value", 0);
$submitButton.prop("disabled", false);
$cancelButton.prop("disabled", true);
}, 100);
})
.fail(c.displayDefaultFailure);
}
});
$useAuth.on("click", function (event) {
var disabled = !($(event.target).prop("checked"));
$.each([$startTLS, $username, $password], function (index, $element) {
$element.prop("disabled", disabled);
});
});
$("form").on("submit", function (event) {
event.preventDefault();
if (!c.checkSessionValid()) return;
var dataRecordIds = $("#datarecords").val(),
templateId = $("#designtemplate").val(),
section = $("#section").val().trim();
var getFinalResult = function () {
/* Get Result of Operation */
$.ajax({
type: "POST",
url: "/rest/serverengine/workflow/contentcreation/email/getResult/" + operationId
})
.done(function (response, status, request) {
c.displayHeading("Operation Result");
c.displaySubResult("Email Report", response);
})
.fail(c.displayDefaultFailure);
};
/* Construct JSON Identifier List (with Email Parameters) */
var config = {
"sender": $("#sender").val(),
"host": $("#host").val(),
"useAuth" : $useAuth.prop("checked"),
"useSender": $("#usesender").prop("checked"),
"attachWebPage": $("#attachweb").prop("checked"),
"attachPdfPage": $("#attachpdf").prop("checked")
},
senderName = $("#sendername").val().trim(),
drids = c.plainIDListToJson(dataRecordIds);
if (senderName.length) config.senderName = senderName;
if (config.useAuth) {
config.useStartTLS = $startTLS.prop("checked");
config.user = $username.val();
config.password = $password.val();
} else {
config.user = "";
}
config.identifiers = drids.identifiers;
/* Process Content Creation (By Data Record) (JSON) */
var settings = {
type: "POST",
url: "/rest/serverengine/workflow/contentcreation/email/" + templateId,
data: JSON.stringify(config),
contentType: "application/json; charset=utf-8"
};
if (section.length) settings.url += "?section=" + section;
$.ajax(settings)
.done(function (response, status, request) {
var progress = null;
operationId = request.getResponseHeader("operationId");
$submitButton.prop("disabled", true);
$cancelButton.prop("disabled", false);
c.displayStatus("Content Creation Operation Successfully Submitted");
c.displayResult("Operation ID", operationId);
var getProgress = function () {
if (operationId !== null) {
/* Get Progress of Operation */
$.ajax({
type: "GET",
cache: false,
url: "/rest/serverengine/workflow/contentcreation/email/getProgress/" + operationId
})
.done(function (response, status, request) {
if (response !== "done") {
if (response !== progress) {
progress = response;
$progressBar.attr("value", progress);
}
setTimeout(getProgress, 1000);
} else {
$progressBar.attr("value", (progress = 100));
c.displayInfo("Operation Completed");
getFinalResult();
operationId = null;
setTimeout(function () {
$progressBar.attr("value", 0);
$submitButton.prop("disabled", false);
$cancelButton.prop("disabled", true);
}, 100);
}
})
.fail(c.displayDefaultFailure);
}
};
getProgress();
})
.fail(c.displayDefaultFailure);
});
});
}(jQuery, Common));
Screenshot & Output
Usage
To run the example you first need to enter a comma delimited list of your Data Record IDs and the Managed File ID or Name of your design template (previously uploaded to the file store) into the appropriate text fields as your inputs.
Next you need to specify the email parameters to use with the content creation operation:
- Section – the section within the Email context of the template to use
- From – the email address to be shown as the sender in the email output
- From Name – the name to be shown as the sender in the email output
- Host – the network address or name of your SMTP mail server through which the emails will be sent. If required, a server port value can also be specified
- Use From as To Address – use the sender address as the receiver address for all emails in the output
- Attach PDF Page to Email – if a Print context exists in the template, create it's output as a PDF and attach it to the email output
- Attach Web Page to Email – if a Web context exists in the template, create it's output as a single HTML web page (with embedded resources) and attach it to email output
Then you need to specify how email security is to be used with the content creation operation:
- Use Authentication – if authentication is to be used with the mail server
- Start TLS – if Transport Layer Security (TLS) is to be used when sending emails
- Username – the username to authenticate/login with
- Password – the password to authenticate/login with
Lastly, select the Submit button to start the content creation operation.
Once the operation has started processing, the Operation ID will be displayed in the Results area and the Cancel button will become enabled, giving you the option to cancel the running operation.
The progress of the operation will be displayed in the progress bar, and once the content creation operation has completed, a report of the emails successfully sent will be returned and displayed to the Results area.
Further Reading
See the Content Creation (Email) Service page of the REST API Reference section for further detail.