Template Execution Example

This is a full coded example on how to execute a template, convert the result to PDF and verifying the result.

Please refer to "Testing with C#" to create portalContext

var templateName = "My Template";
	var templateGroupName = "My Template Group";
	var templateLibraryName = "My Template Library";
	var documentOutputPostActionName = "Convert to PDF";
	var xmlData = @"<root>
                    <FirstName>John</FirstName>
                    <LastName>Smith</LastName>
                    <Location>United States of America</Location>
                  </root>";
	Guid? documentOutputPostActionId = null;

	var template = (from t in portalContext.Templates
					where t.Name == templateName &&
						  t.TemplateGroup.Name == templateGroupName &&
						  t.TemplateGroup.TemplateLibrary.Name == templateLibraryName
					select t).FirstOrDefault();

	if (template != null)
	{
		if (!string.IsNullOrEmpty(documentOutputPostActionName))
		{
			var documentOutputPostAction = (from dopa in portalContext.DocumentOutputPostActions
											where dopa.TemplateLibrary.Name == templateLibraryName &&
												  dopa.Name == documentOutputPostActionName
											select dopa).FirstOrDefault();

			
				documentOutputPostActionId = documentOutputPostAction.DocumentOutputPostActionId;
			
		}

		var templateExecutionInfo = template.Execute(xmlData, null, documentOutputPostActionId).GetValue();

		if (templateExecutionInfo.Success)
		{
			Console.WriteLine("Template Executed successfully");
		}
	}
	else
	{
		Console.WriteLine("Template not found");
	}