In my model-driven app, I have some Word document templates setup in the Power Apps Templates feature. I would like to be able to create a document from one of the templates triggered by JavaScript code. The new document would be populated with values for a specified record. Ultimately, I would have the document returned in the code so I can place it in a folder. Is this possible?
You rule man. The only non-plugin solution of ExportPdfDocument I could find on the interwebz. Even works from within a PCF control with a little bit of tweaking. I wish I could add the code here but my post is failing to submit when I include it.
You can use the exportPdf Action. Here's some example TypeScript code that will return a File object - for uploading, saving, printing, etc.
async function exportPDF(entityRef: Xrm.LookupValue, templateRef: Xrm.LookupValue, primaryEntityTypeCode: number, name: string): Promise<File> {
const r = await Xrm.WebApi.online.execute(new ExportPDFRequest(
primaryEntityTypeCode,
templateRef.id,
entityRef.id,
));
if (!r.ok) {
throw Error(`"Response not ok: ${r.status} ${r.statusText}`);
}
const rBody = <{ PdfFile: string }>await r.json();
const pdfFile = new File([base64ToByteArray(rBody.PdfFile)], `${name}.pdf`, { type: 'application/pdf' });
return pdfFile;
}
function base64ToByteArray(base64: string) {
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}
class ExportPDFRequest {
public EntityTypeCode: number
public SelectedTemplate: object;
public SelectedRecords: string;
constructor(
entityTypeCode: number,
documentTemplateID: string,
selectedRecords: string,
) {
this.EntityTypeCode = entityTypeCode;
this.SelectedTemplate = {
"@odata.type": "Microsoft.Dynamics.CRM.documenttemplate",
documenttemplateid: documentTemplateID,
};
this.SelectedRecords = JSON.stringify([selectedRecords]);
}
public getMetadata() {
return {
boundParameter: null, // not bound
parameterTypes: {
EntityTypeCode: {
typeName: 'Edm.Int32',
structuralProperty: 1, // PrimitiveType
},
SelectedTemplate: {
structuralProperty: 5, // Entity Type - don't validate just pass as object
},
SelectedRecords: {
typeName: 'Edm.String',
structuralProperty: 1, // PrimitiveType
},
},
operationType: 0, // action
operationName: "ExportPdfDocument"
};
}
}
You could also consider calling the same API from Power Automate -> Generate PDF from Document Templates in CDS & Dynamics 365 CE using native Web API with Flow - There's Something About Dynamics 365 (2die4it.com)
WarrenBelz
109
Most Valuable Professional
Michael E. Gernaey
82
Super User 2025 Season 1
MS.Ragavendar
72