Showing posts with label Power Automate Flow to Create a Folder and Move Attachments to SharePoint site location. Show all posts
Showing posts with label Power Automate Flow to Create a Folder and Move Attachments to SharePoint site location. Show all posts

Sunday, May 29, 2022

Power Automate Flow to Create a Folder and Move Attachments to SharePoint site location

In this blog, I will cover step by step process of writing a common Power Automate flow and I will handle the below business requirements.

1. Write a Power Automate Flow to Create a Folder on the SharePoint site.
2.  Common Child Power Automate flow to Move attachments to the SharePoint location.
3. Move Email attachments to the SharePoint site.
4. Move Case, Account, etc. attachments to the SharePoint site.

Basically for the above scenario, I have created a common child flow and I will be calling this flow through my Parent flow. Which I have shared at the end of this blog.

This is how the entire flow will look once it's created.



Let's begin. Follow the step one by one...

Note: Before that, First create a solution file -> then inside the solution create a Child flows. It should be created within the solution.

Create a solution as shown:



1. Open the solution and create a power automate flow as shown



Click on Instant Flow and give a name to your flow and select Manually trigger a flow -> Click Create


2. Add all the required input variables to your child's flow.

Add input variables to your child's flow as shown


Note: I have added below 5 input variables that seem common to me. I will pass the value for these variables from the parent flow from where this child flow will trigger.

a) 
SharePointFolderName -> It is SharePoint folder name. Pass the value to this variable from the parent flow. b) RelativeURL -> This variable will hold document location. It's the SharePoint Document Location in the D365 to map the records in D365 & SharePoint site.
c) SharePointListOrLibraryName -> This is Library name In SharePoint where attachments can be uploaded.

d) EntityId -> Pass the entity GUID to this variable.
e) EntityLogicalName -> Pass entity logical name from Parent flow.
3.  Add list rows (List SharePoint site-URL) -> Select table name, Column, Filter rows

[This step is to retrieve the SharePoint site absolute URL which will be used in the next step]


4.  Add -> Create a new folder in the SharePoint site

[Using this step to create a new folder in the SharePoint site location to store the attachments within this new folder.
I'm creating this new folder dynamically each time whenever new records are getting created and in the next step I will place attachments within this folder]


Site Address -> 
first(outputs('List_SharePoint_site_-_URL')?['body/value'])?['absoluteurl']
List or Library->
Pass input variable (SharePointListOrLibraryName )

Folder Path -> Pass input variable (SharePointFolderName)

5.  Add -> List rows

[To retrieve the sharepointdoumentlocationid based on the below filter condition. I need this in my next step to create a SharePoint document location]

Table name -> Document Location
Select Column -> sharepointdocumentlocationid
Filter rows -> 
relativeurl eq '@{triggerBody()['text_1']}' and startswith(parentsiteorlocation_sharepointsite/absoluteurl, 'http')


6. Add a Switch condition

[To check based on the type of entity and direct it to the entity it belongs to -> if incident then it will go to the Incident Document location if Account then Account Document Location else go to default]



7.  Add a new row 

[To add case-related attachments to the incident location.]



Table name -> Select Document Locations
Name -> Documents for "SharePointFoldername"
Parent Site or Location (Document Locations) ->
sharepointdocumentlocations(@{first(outputs('List_Document_Location_-_Parent_Document_Location_for_Records')?['body/value'])?['sharepointdocumentlocationid']})

Regarding (Cases) -> Pass Entity Logical Name & Entity Id.

\@{triggerBody()['text_4']}(@{triggerBody()['text_3']})

Relative URL -> SharePointFolderName 


Similarly, add it to the account entity record.

Once both are added It will look as shown below:

8. Add -> Get a row

[To retrieve attachments based on filter criteria as shown from the Attachments table]



9. Add a condition ->
[To check to get attachment body is not empty (empty(outputs('Get_Attachments')?['body']))  and  (EntityLogicalName is not Null)]





10. If the above conditions are true  -> Then Create an Attachments file in the SharePoint site location

Site Address: 
first(outputs('List_SharePoint_site_-_URL')?['body/value'])?['absoluteurl']

Folder Path: Pass the input variables as shown
File Name:

File Content:
base64ToBinary(items('Apply_to_each')?['body'])

Converting from base 64 to binary.

11. Once All are done the last step is to empty the D365 attachment file. Delete the file from the D365 Attachment Location
Table Name: Attachments
Row ID:





12. Pass a response to the parent flow


That's all,
We have successfully created a common child flow to create a folder in the SharePoint site and attach the attachments to the same folder.

Note: Now next step is to create a Parent flow so that this child flow can be triggered. This Child flow can be triggered:  Below is the Parent Flow Link URL ->

Power Automate Flow to Move Email Attachments to SharePoint site

1. When receiving Attachment through Email
2. When a Case, Account, or any entity is Created/modified as per your business requirement you can trigger it and update it accordingly.