cancel
Showing results for 
Search instead for 
Did you mean: 
EmadBeshai

How to Create a Power Automate Flow to Populate a Word Template and Download the Result from a Model-Driven App?

If you want to create a button in a Model-Driven App to download a Word template in different formats (DOCX and PDF), follow these steps:

1. Create a Power Automate flow with an HTTP trigger.

EmadBeshai_0-1716709854762.png

 2.Add a step for Word template population.

EmadBeshai_1-1716710231525.png

 

3. After that check if you want it to be with docx format or pdf

EmadBeshai_2-1716710380591.png

4. Create new file for the populated word template on SharePoint as shown below

EmadBeshai_3-1716710459039.png

5. If you want to convert the file to PDF add the below step then create new file at the SharePoint from the word template content.

EmadBeshai_4-1716710542034.png

6. Get the file content from SharePoint and pass the values to get content step and pass the content to variable with the demonstrated expression

EmadBeshai_5-1716710921121.png

7. Finally pass the variable to the response of the flow

EmadBeshai_6-1716711020965.png

7. You can use the below JS code to download the File returned from the Power Automate Flow

 

 

 

 

function CallPrintFlow(flowURL, ids, FileType) {
(async () => {

					const rawResponse = await fetch(flowURL, {
						method: 'POST',

						headers: {

							'Accept': 'application/json',

							'Content-Type': 'application/json'

						},

						body: JSON.stringify(

							{
								id: ids,
								FileType: FileType

							})

					}).then(response => response.text())
						.then(data => {

							//_base64ToArrayBuffer(data);
							DownloadPDF(data, FileType);



						}).catch(Error => {
							Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");

							//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
							/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
							Xrm.Utility.closeProgressIndicator();
						});

				})();

			} else {
				console.log(this.responseText);
				Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");

				//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
				/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
				Xrm.Utility.closeProgressIndicator();
			}
		}
	};
	req.send();

}

function DownloadPDF(data,fileType) {
	let str = data;
	if (fileType == "PDF")
		var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".pdf";
	else if (fileType == "Word")
		var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".docx";

	var a = document.createElement("a");
	document.body.appendChild(a);
	a.style = "display: none";
	a.href = str;
	a.download = filename;
	a.click();
	Xrm.Utility.closeProgressIndicator();

}

 

 

 

 

 

If this post helps you with your problem, please give it a Thumbs Up. 

Comments
About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/