Change SharePoint attachment name using Power Automate

Publication date: 2021-06-01
sharepoint_attachements_name

Imagine a scenario in which we use a Power Apps application connected to a SharePoint list to record expenses during a business trip. Each of the expenses must be documented by attaching an appropriate file to a record.

Due to business requirements, we want each attachment to have a name that follows the pattern:

RecordID_OriginalNameOfTheAttachment

The problem is that application users sometimes forget about it and add an attachment that does not meet this criterion. How can we achieve the desired effect?

One method is to use Power Automate and create a flow to rename the attachment in the list. In our case, the flow is called from within Power Apps using the Run function. As a parameter, we pass the ID of the record affected by the change.

To save the attachment in Power Apps we use the attachment form card and the SubmitForm function. Afterward we use the Patch function and the LastSubmit method to save the data to a specific record.

A record goes through several approval stages, at which additional attachments can be added.

In order for the flow to work from the beginning (i.e. when adding a completely new record to SharePoint list), we need to add the UpdateContext function between the SubmitForm function and the function that runs the flow. It will assign the ID of the record that we previously added to the local variable locRecordID.

Our formula might look like this:

SubmitForm(Application);  // Send form to SharePoint list

UpdateContext({locRecordID: Application.LastSubmit.ID});  // Create contex variable with record ID as its value

‘ChangeAttachmentName’.Run(locRecordID) // Run flow with record ID as a parameter

Now let’s prepare a flow that will rename attachments.

We create instant type flow ChangeAttachmentName. We use PowerApps as the trigger.

Then we download the record, the attachments of which will be changed (Get item action):

In our case the list is named Wniosek and it is located in SandboxIgora site.

In the next step, we use the Get attachments action and we select previously initialized recordID (wniosekID) variable:

We then create an Apply to each loop with the condition “DisplayName starts with recordID (wniosekID)”:

Thanks to this, we check whether the name of the attachment begins with the ID of the record (a scenario in which user has saved the attachment in accordance with the requirements described above is possible).

In a branch of the condition, we have:

If the name of an attachment begins with an application ID, we do nothing.

If it is otherwise, we download the content of the attachment (Get attachment content) and add the attachment as File Name by entering the formula:

concat(variables(‘recordID’), ‘_’, items(‘Apply_to_each’)?[‘DisplayName’])

(In May 2021, when this article was created, I had to copy “items (‘Apply_to_each’)? [‘DisplayName’]” because in expressions I couldn’t add it from dynamic content – just watch out for the “@” sign).

In case of the attachment, which so far has been called train_ticket.pdf, we will get the name 1_train_ticket.pdf (assuming that we are working on a record with ID equal to 1).

Suppose we started with three attachments to a record with an ID equal to 1:

train_ticket.pdf

hotel.pdf

lunch.pdf

Now we should have six attachments:

train_ticket.pdf

hotel.pdf

lunch.pdf

1_train_ticket.pdf

2_hotel.pdf

3_lunch.pdf

The previously used loop just added new files with the appropriate name. We now need to remove any attachments that do not meet the naming criteria. We do this by creating an additional loop:

This time, we simply apply the Delete attachment action to the attachments which name does not start with the record ID.

After that loop executes, we should have three attachments:

1_train_ticket.pdf

2_hotel.pdf

3_lunch.pdf

The above example is not the only way to rename an attachment.

We could also use When an item is created as a trigger, but our scenario assumed that the application could be edited later during approval stages. At these stages new attachments might be added.

So how do you check if that is the case and if the flow should run?

We need to make a minor modification in Power Apps. We add the function to the OnAddFile property of the attachment card:

UpdateContext({fileWasAdded: true})

and on the OnSelect property of the button sending the Patch to the list we add:

If(fileWasAdded, ‘ChangeAttachmentName’.Run(selectedItem.ID))

In this case, “selectedItem” refers to the record selected from the gallery on the previous screen.

 Thanks to the above change, we are sure that flow will start only when we actually add a new attachment to the record at further stages of approval.