Custom XML data for Mustache template Mail Merge using Aspose.Words REST API

Hi there. I’m in the process of testing mailmerge and have tested mailmerge template and mustache template. I’m wondering how i can make a custom xml for placeholders that doesn’t involve “orders” as in the mustache template. I would like to test the mustache template with single field values with no repetition of any sort. Any help is welcome.

@Softwareselskabet

Thank you for contacting Aspose Support.

Please check ExecuteMailMerge example that is using SampleMailMergeTemplateData.txt as MailMerge template and SampleMailMergeTemplate.docx as source document and produce TestPostDocumentExecuteMailMerge.docx output document.

As desired this MailMerge template is using single field values with no repetition.

Aspose.Words for Cloud API Reference allow you to test our APIs directly in the browser. Please check our GitHub repository for SDKs for different platforms.

Just to clear further POST /words/{name}/executeTemplate works with Mustache Templates and POST /words/{name}/executeMailMerge works with MailMerge templates.

If you are only interested in Mustache templates, do let us know we will share an example of Mustache templates.

P.S. Files are zipped because Discourse does not allow to upload .txt and .doc files directly.

Hi @mateensajjad. Thanks for the quick respons.

Yes we have succesfully tested the single field mail merge examples in the sdk .net as well as the Mustache templete. We have used TestPostExecuteTemplate and TestPostDocumentExecuteMailMerge.

We want to use the Mustache templete solution but with single fields and without “foreach”.

Does this mean we have to call new PostExecuteTemplateRequest(remoteName, data, this.dataFolder, destFileName: destFileName) but without regions? And can you provide an example of the request as well as a template and XML.

We appriciate any help.

Thanks again.

@mateensajjad

We are writing Mustache Templates example and will share with you shortly.

Hi @mateensajjad

I am impressed with your support. We are really looking forward to use your solution in production.

@Softwareselskabet

withRegions is the parameter that defines whether the API expects template document to contain foreach element or not. Its default value is true. We are interested in Mustache templates without foreach, so we will set withRegions parameter to false. So the code will look like this

public void postExecuteTemplate() {
    var localName = "PostExecuteMustacheTemplate.docx";
    var remoteName = "PostExecuteMustacheTemplate.docx";

    var fullName = Path.Combine(this.dataFolder, remoteName);
    var destFileName = Path.Combine(BaseTestOutPath, remoteName);
    var data = File.ReadAllText(BaseTestContext.GetDataDir(this.mailMergeFolder) + "DataFile.txt");
    
    this.StorageApi.PutCreate(fullName, null, null, File.ReadAllBytes(BaseTestContext.GetDataDir(this.mailMergeFolder) + localName));

    var request = new PostExecuteTemplateRequest(remoteName, data, this.dataFolder, withRegions: false, destFileName: destFileName);
    var actual = this.WordsApi.PostExecuteTemplate(request);
}

Mustache Template File: PostExecuteMustacheTemplate.docx.zip (46.3 KB)
Data File: DataFile.txt.zip (1.5 KB)
Output File: PostExecuteMustacheTemplate_Output.docx.zip (15.8 KB)

The data file can either be XML or JSON.

@mateensajjad

Worked like a charm!

Do we need to change anything if we want to use JSON?

@Softwareselskabet

Do we need to change anything if we want to use JSON?

Nothing. A sample JSON file is attached: DataFile.json.zip (700 Bytes)

@mateensajjad Cool! Thanks. And we can also use JSON “with regions”, right?

@Softwareselskabet

And we can also use JSON “with regions”, right?

Yes.

@Softwareselskabet

Please note in the latest API Version V4.0, we have combined the Mail Merge and Mustache Template APIs. Now the same MailMerge API will be used for both types of Templates. It is recommended to use the latest API Version, as it is better than old versions in terms of memory management and API structure.