Recently Nick commented on my “How to send an email and create a task in Microsoft Planner?” article, mentioning that I don’t have an article on how to add all email attachments to Planner, so today we’re going to solve that. The idea is simple. We get an email with attachments, and we want to add all of them to a Microsoft Planner as attachments to a task. Let’s see how to add all email attachments to Planner using Power Automate.
The trigger
In order not to process all emails that we get and add them to Planner, we’re going to add a prefix to the header of the email to indicate Power Automate that only those should be processed. Also, we’ll add a filter to the “When a new email arrives” trigger to ensure that the Flow will only fire for the emails we want to process. This way, we don’t need to add any more filters to the Flow, and we will save Flow runs that are important if you’re using a free account and stricter limitations on how many Flows can be run. Also, it’s a lot easier to debug since the history will only contain items we intended to add to the process and not all emails we received.
Here’s what the trigger will look like:
In our example, we’re adding the string “[task]” to ensure that only these emails are processed. Also, I’m checking the “Only with Attachments” to “Yes” because I only want to process emails with attachments. Still, in your case, you can process all emails you want to add to Planner and, optionally, add the attachments if they exist. It’s up to you how you want to do it.
Finally, there’s the “Include Attachments” tag. I will keep it disabled because even if you enable this feature, Flow (at the time of writing this article) won’t fetch the attachments correctly. I go into detail in this article, but you need to know that you need to fetch the articles separately using the “Get email” action.
Getting all attachments
Now that we have the trigger properly configured let’s look at how to get the attachments. As mentioned before, we get the attachments by using the “Get email” action, like this:
Notice that we’re using the “Include Attachments” as “Yes” to get the attachments’ contents.
Saving all attachments
Now that we have all attachments, we need to save them somewhere. This is because Planner won’t store the files, so we need to upload the files to a location (OneDrive for Business or SharePoint, for example). Although it looks like Planner has the files attached, what it will do is upload the files to the root of the SharePoint documents default library if the plan was created there, or if you created it in Teams, for example, it would upload to the root of the document default library there as well.
I don’t like this behavior, and it’s not consistent with Microsoft Forms , where it creates a new folder called “Apps” > “Microsoft Forms” and then creates a folder per Form. This way, things are nice and organized, and the files won’t pollute your root directory. Let’s look at one where I created only a few tasks and see what it looks like:
After a few hundred tasks, the document library would be unusable because it will have a huge number of files, so we’ll see in the steps below how to solve that.
As mentioned before, since the Planner won’t store the files, we need to save them somewhere to be referenced in the Planner’s task. We have two ways to do it:
- According to Microsoft, the “correct” place is where you upload to the document library where the plan was created.
- Your OneDrive for Business, where you can organize them properly but need extra action to ensure that the attachments are visible to everyone.
Let’s take a look at both approaches.
Save to the “correct” place
We can use a simple “Apply to Each” action and the SharePoint “Create file” action to save the file. Here’s what it looks like:
With this, we’ll save all files for that email in the default folder, defined in the previous example as “Documentos Partilhados”, but depending on the site’s language, you’ll see a different name.
Since we’ll be fetching a link, you can define the path that you want to save them. Take this opportunity to consider where the files should be to keep things organized. Think as well that no one should be able to remove them, to avoid issues in the task.
OneDrive for Business
We can do it with a simple “Apply to Each” action with a “Create File” action like this:
Notice that we’re using OneDrive for Business “Create file” action to save the files into a temporary location before we can add them to Planner. You can and should pick a better place for your files so that you can keep things organized.
Prepare the attachments
Now that we have the attachments uploaded, we need to prepare them to upload. We will use the data in future actions, but we must keep track of the attachments uploaded to add them to the task. We can use the same “Apply to each” action to keep track and an array variable to keep the data. But what data do we need? We can get that information from the action we’re going to use. The Planner “Get task detail” action, by adding some dummy data and using the toggle like this:
Now we know that, for each attachment, we need:
[
{
"alias": "alias",
"resourceLink": "https://manueltgomes.com/",
"type": "Other"
}
]
Of course, the “resourceLink” won’t be my website, and the “alias” won’t be called “alias”. This is an excellent way to understand what we need. So let’s add a variable and when we get a new attachment, let’s add it to the variable. You can use the “Initialize Variable” action as follows.
Don’t forget to pick “Array” otherwise; it won’t work.
Now let’s update the variable. I’ll split it up into two subsections depending on your choice in the previous section:
SharePoint
If you picked SharePoint, then you can use SharePoint’s “Create sharing link for a file or folder” to get the link to the file like this:
Now let’s use the “Append to array variable” action to build the previous object like this:
We’re using the file’s name as the alias to display it in Planner’s task and the link to the file we got from the previous action.
OneDrive for Business
The OneDrive for business strategy will be the same, but the action will be adjusted to get the link from OneDrive for Business by using the “Create share link” like this:
Now let’s use the “Append to array variable” action to build the previous object like this:
We’re using the file’s name as the alias to display it in the Planner task and the link to the file we got from the previous action.
Create the task
Now that we have the files saved, let’s create the task. This is the most straightforward part of the Flow since we have a “Create Task” action. Here’s an example:
In the example above, I’m adding the subject as the title of the task, but you can use something else that makes sense.
Add the attachments to the task.
Now that we have a task let’s add all email attachments to Planner. It’s impossible to create a task with attachments in one step, so we need to use the “Update task details” action. As we’ve seen before, we can use the “References” to add attachments, but we need to prepare the array’s contents to add it as a reference. To do that, we need three functions. The JSON, concat, and join functions. Here’s the complete formula:
json(concat('[',join(variables('ATTACHMENTS'),','),']'))
It may look complex, but it’s not. Let’s explore each section from the inside out:
- Join – will transform the array into a string separated by the character we define (in our case, “,”).
- concat – will generate a string from multiple strings. We want the final result to be an array, so we need to add them to the string.
- JSON – will convert the string to a valid JSON. We need to provide a JSON to the “Update task details” action , so we need to convert the string; otherwise, we’ll have issues.
Now we only need to add it to the “Update task details” action like this:
That’s it. The task will be created with all attachments.
Final thoughts
It may look like many steps, but it seems much more manageable when you break them down into smaller pieces. I like this approach because we can automate the process of creating tasks and, at the same time, add all email attachments to Planner. It’s a huge time saver for you and your coworkers in case they know where to send the emails and the format.
Photo by Felipe Furtado on Unsplash
Hello Manuel, truly a great idea and also a good guide. However, I can’t get the workflow to run on my end, and I think it’s due to incorrect handling of the loops. Could you send me an export of your workflow or share a screenshot? Greetings from Austria.
Hi Manuel,
When i try this – i get this error;
Flow save failed with code ‘WorkflowRunActionInputsInvalidProperty’ and message ‘The inputs of workflow run action ‘Append_to_array_variable’ of type ‘AppendToArrayVariable’ are not valid. The provided value of ‘[ { “alias”: “@{items(‘Apply_to_each’)?[‘name’]}”, “resourceLink”: “@{outputs(‘Create_sharing_link_for_a_file_or_folder’)?[‘body/link/webUrl’]}”, “type”: “Other” } ]’ of type ‘Array’ must be a primitive type, or a valid template language expression.’.
Maybe you can help me?
Best Regards
Rasmus
Use This
{
“alias”: “alias”,
“resourceLink”: “https://manueltgomes.com/”,
“type”: “Other”
}