Thursday, April 15, 2021

Export To Excel Using Power Automate

Hello Friends,
Welcome back with another post on Power Automate. Today, we will try to export SharePoint Online list data to Excel. Let's start-
  1. First of all we need a SharePoint list with records that needs to be exported. I had created one list "ExportToExcelList" and added 3 records.


  2. Now create an excel template with same columns and convert it into table and upload it to OneDrive. The access to OneDrive is mentioned in my previous post from Access OneDrive, steps 78-81. I created folder PowerApps >> Templates.

  3.  


  4. Now there are 3 ways to create a flow-
    1. When an item is created
    2. When an item is created or modified
    3. Scheduled flow
  5. I am creating a flow upon item create. Here, I will
    1. Get the template
    2. Check if template has rows
    3. If yes, delete those rows
    4. Write new rows
    5. Send it my email
  6. Let's do step by step-
  7. Create a flow on "When an item is created". You may see how to create the flow on my post Create PDF Using Power Automate, steps 10 - 24.
  8. Here we will use Flow name as "ExportToExcelOnItemCreate" and the list is "ExportToExcelList".


  9. Now before moving ahead, there are 2 things-
    1. You need to remove the entire content and rewrite
    2. You need to append the new row 
  10. In case you need to remove the entire content, follow the steps given below from 11 - 
  11. Click on "+ New step" and choose "List rows present in table (Excel Online (Business))"
  12. Set the Location, Document Library, File, Table.


  13. Now check if rows are present in table. For this add a Condition and check on length.


  14. If there are rows, delete them. For this, add and action in "If yes" condition and choose "Delete a row Excel Online (Business)".
  15. Set Location, Document Library, File, Table, Key Column (mention column "Name" from the Excel Table), Key Value (choose column value "Name" from the Excel Table).


  16. "Apply to each" action will get auto added as soon as you will choose Key Value.
  17. Once all rows get deleted, move to next step. Now, the important point here is that Deletion/Insertion of rows in OneDrive Excel file is an Asynchronous process. Therefore, it is better to put a delay of sometime before moving to next step so that all async process gets completed.
  18. Add a Delay action. for this click on "+ New step" and choose "Delay (Schedule)".


  19. Now we will get items from our SharePoint list. I just added a basic filter query upon Name column that get only those items where it is not blank.


  20. Add a condition to check if SharePoint list has items-


  21. If items are available start writing into excel. For this, choose action "Add a row into a table (Excel Online (Business))" and fill in the information as below-


  22. If you wish to add another delay for a minute, you can add in the Apply to each section as shown below (however it's purely optional. I added a delay of 30 seconds in below example)-


  23. Once all items get written in excel, you can add another delay to get all async write process complete-


  24. Now we will get the content of this excel file and send it as an attachment over email. to get the content choose action "Get file content (OneDrive for Business)"-


  25. Select the Excel template file in "File"path box-


  26. It's almost done. Now send this file over email as below-


  27. It's all done. Now save the flow and put a new entry in SharePoint list.


  28. Let's wait for the flow to run.
  29. Wow! I got the mail with attachment-




  30. Let's try with adding one more item.


  31. The final list of items is-


  32. Got mail for this transaction also-


  33. As expected, the flow has replaced all the items with fresh list-


  34. This way, we can export our SharePoint list items to excel and can send over email.
  35. Additional feature that you can add is that you can save this excel in your SharePoint library also.
  36. Add an action "Create file (Sharepoint)". Provide details, save it and Test the flow-




  37. Let's wait for the result. In case, if the flow throws error of "File Content is null", just add action "Get File Content" (steps 36 - 39) just before the Create File action (step 58)and use this File content in creating file in SharePoint.
  38. Meanwhile, I was writing the functionality, I got the mail of file getting generated. When I checked the SharePoint library, the file got created there also. Below are the screenshots-






  39. So, this way, you can achieve the discussed functionality. Hope you enjoyed it.

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.