Welcome back with another post on Power Automate. Last year, we have learned about Export To Excel Using Power Automate where we have used OneDrive to store the excel template. Recently, someone asked me provide the same feature without using OneDrive. In short, their requirement was- whenever an item (request) is created in SharePoint list, the information is to be emailed in the form of Excel file to the user and the same excel file needs to be stored in a document library for future reference. Let's begin-
- Let's create a SharePoint list "TestList". It is having "Title" column by default.
- Below is the design of Excel template. It is having a table "Table1" with 2 columns "ID" & "Title".
- Below is the structure of document library "ExcelLibrary" where we will save the template and the files.
- The root library is "ExcelLibrary". It is having 2 nested folders -
- Template - It will hold the Excel template.
- DataFiles - It will be used to save the final excel files for each request.
- Upload the Excel template in "ExcelLibrary" >> "Template" folder as shown in above screenshot.
- Now, we will write the Power Automate Flow. Open the Power Automate Portal and click on "+ Create".
- Select "Automated cloud flow". Give flow a suitable name and choose the flow trigger as "When an item is created (SharePoint)".
- Select the Site Address and List Name. Here list name will be "TestList".
- Now, we will copy the excel template from Template folder to DataFiles folder. For this, we will choose "Copy file (SharePoint)" action.
- Select the Current Site Address, File to Copy (the excel template file), Destination Site Address (it will remain same as we are copying to same site), Destination Folder (ExcelLibrary >> DataFiles).
- Now the important point here is to select the course of action if another file is already there with same name. Here we will choose to Copy the file with new name. Basically, the flow will append an incremental number ahead to the file name and save. It will save us from replacing the existing file of another request.
- Save the flow and keep saving the flow at regular interval in order to save your precious efforts.
- Now, add another action to add row in excel file. For this choose the action name "Add a row into a table (Excel Online (Business))".
- Select the Location, Document Library.
- For File, select the "Id" property of "Copy file" action from dynamic expression.
- For Table, it will not show any table name because, you are providing the file dynamically. Therefore, choose "Enter custom value" and input "Table1". If you remember, you had added a table in Excel Template, if you check the same of this table, you will find it Table1. Otherwise, whatever be the table name, you have to put in here.
- Now, there will be another filed get displayed called "Row".
- As, all the values are dynamic in nature, therefore in design time, power automate is unable to fetch the schema of the table as well. Therefore, it allows us to provide the data to be inserted in JSON format.
{ "ID": @{triggerOutputs()?['body/ID']}, "Title": "@{triggerOutputs()?['body/Title']}" }
- The next step is to get the content of this file and send it through Email. Before that, we will add a delay of around 1 minute so that the writing of data gets completed in excel file.
- Now, add the Get file content (SharePoint).
- Select the Site Address. File Identifier will again picked from Copy file "Id" property.
- Now, send an email using "Send an email (V2) (Office 365 Outlook)" action.
- Here, we can give the file name as per our wish. I had created a combination of "Created By Display Name" (replaced spaces by Underscore), suffixed by ItemID.
concat(replace(triggerOutputs()?['body/Author/DisplayName'],' ','_'),'_',triggerOutputs()?['body/ID'],'.xlsx')
- Now, the attachment content will be mapped with the File Content property of Get file content action.
- Now the next step is to rename the copied file in DataFiles folder with same name we used in attachment. But that is not so easy because, the file copied and written content in it gets locked as we were editing (adding items in that file) the file. Therefore, the file will not get renamed.
- For this, we have to wait to get it unlocked. Here we will use "Do until" action. Let add the steps for it-
- Add "Initialize variable" action -
- Name: blnIsFileLocked
- Type: Boolean
- Value: true
- Add "Do until" action -
- Change the limits as shown below.
- Add "Delay" action and set count to 1 and unit to Minute.
- Now, we will try to rename the file. If the file is unlocked, it will get renamed otherwise, it will throw error. We have to handle the error carefully. For this, you may use the property "Configure run after".
- Add "Send an HTTP request to SharePoint" action.
- Fill up the required details. Here, we are using "ItemId" attribute to fetch the desired file.
- For Body section, again it will take JSON and the FileLeafRef property is set to the file name we had used earlier (without .xlsx).
{'__metadata':{'type':'SP.Data.ExcelLibraryItem'},'FileLeafRef':'@{concat(replace(triggerOutputs()?['body/Author/DisplayName'],' ','_'),'_',triggerOutputs()?['body/ID'])}'}
- Add "Set variable" action and set the variable "blnIsFileLocked" to false.
- Now, we will set the "Configure run after" property for this action. Click on ellipses (marked in above screenshot) and choose "Configure run after".
- It will show the settings as above screenshot. By default, "is successful" is selected. This is what, we require. This action should only execute if the file get renamed means the HTTP request get executed successfully. Click on "Done".
- Now, add "Get file properties (SharePoint)" action to get the properties of the file. You may skip this step. I am adding this step to send another mail with the link of the excel file for future reference. For each step you have to set the "Configure run after" property (for "is successful")
- The last step is to add action "Send an email (V2)".
- The Item Link is set as-
concat('<a href="',split(outputs('Get_file_properties')?['body/{Link}'],'?')[0],'">Item Link</a>')
- Set "Configure run after" for "is successful".
- At last, you may add "Terminate" action with Status as "Succeeded".
- Let's start testing.
- Add an item in TestList.
- As checked, file has been created in DataFiles folder with content as below.
- Now, we are waiting to get file renamed.
- Finally, the workflow got completed successfully. Let's see, if the file name has been changed. Yes, it's done.
- And the mails, I received are-
- This way, you can achieve the functionality.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !