Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Answered

Can I create a document from a template in JavaScript?

(0) ShareShare
ReportReport
Posted on by 306

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?

  • vdblohowiak Profile Picture
    Microsoft Employee on at
    Re: Can I create a document from a template in JavaScript?

    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.

  • Verified answer
    jm125245 Profile Picture
    on at
    Re: Can I create a document from a template in JavaScript?

    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)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 109 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 82 Super User 2025 Season 1

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 72

Overall leaderboard