Saving an email attachment to a non-existent folder within SharePoint
Do you receive emails with attachments that you want to save into SharePoint regularly? I do, and one of the things that I needed to overcome when doing this was updating the folder for each month. What do I mean by that? Well, I usually save my files into a folder with a path as follows:
Part of path that never changes/year/month
E.g. share documents/folder 1/folder 2/2018/12 – December
As you can imagine, that path changes every month and I can’t hard code it into flow. I did some research into this and found people who had managed to get this working, but it was confusing and complicated. What I did learn in the whole process though, was that the path you put into the create file can be whatever you want, and it will create that. So, the question I needed to answer was, “how do I create the path with the date in it?” Let’s break it down.
Because in this example I’m working with an email that has an attachment, that’s my first step – create a trigger that gets the email. In this particular case it’s a PDF containing a backup report. I only want emails that have attachments and I want it to include the attachments. Without including the attachments, I won’t be able to save them.
Then I need to compose the date section of my path. In this case that’s the 2018/12 – December part. To do this I use a compose action with the following input:
Useful when it hides the actual string used to construct it, I know. However, by clicking on it you can view it on the expression tab. It looks like this:
formatdatetime(triggerBody()?['DateTimeReceived'],'yyyy/MM - MMMM')
The ‘DateTimeReceived’ variable shows up as Received Time in the Dynamic content list as part of the output from the when a new email arrives trigger.
You can see what the variable name is by holding your mouse over the variable and letting the tooltip pop up.
Once I’ve composed the date, I then need to add it to the rest of the path where I want to save the file. I do this using an initialise variable action.
The value looks like the following:
Shared documents/fixed part of the path/output
Where output is the dynamic variable output from the compose action immediately before this. The last part of the process is to save the attachments.
Here you can see I have set the Folder Path to the FilePath variable that we created prior. You’ll also note that there is a foreach for this. When I created this, I just added the create file action and then dragged the Attachments Name variable into the File Name section.
Because Attachments can have multiple items within it, it automatically creates the apply to each. When that happens, you’ll notice that an apply to each current Item variable appears and most of the rest disappear. You can bring them back by clicking See more at the bottom.