Discussion -

0

Discussion -

0

Change SharePoint attachment name using Power Automate

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.

Change attachment name using Power automate

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

imaged - Change SharePoint attachment name using Power Automate

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

imagee - Change SharePoint attachment name using Power Automate

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:

imagef - Change SharePoint attachment name using Power Automate

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

image10 - Change SharePoint attachment name using Power Automate

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:

image11 - Change SharePoint attachment name using Power Automate

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:

image12 - Change SharePoint attachment name using Power Automate

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

Summary

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.

Developico

Developico is a group of people with passion to technology. Especially to Low-Code / No-Code solutions.

0 Comments

Submit a Comment

Your email address will not be published.

About the end of Excel

About the end of Excel

Some call it a king, others a fallen angel. Microsoft Excel. History knows brave people who, not recognizing other rulers, served only him. Presentation? No problem. Project management? Of course. Business application? Here you are. It sounds odd, but that’s often the case.

Array Sorting in Power Automate

Array Sorting in Power Automate

Have you ever tried to find a function in Power Automate that sorts arrays? If you did, you most likely know that it isn’t there. But it is impossible?

Passwords and why they suck

Passwords and why they suck

A lot of history here, a lot of data loss, a lot of battles with your users. Passwords are essential in modern world or at least it seems so. So what is going on here?