Retrieve parent folder name of item

Retrieving the parent folder name of an item in a SharePoint list within Power Automate can be challenging. Especially when the folder path contains multiple folders. This article will show how you can use the REST API for this.

Inspiration

This question from JDeng:

I have created a trigger to go alert me when a new file is added to my SharePoint. I am alerting myself by sending a message to my Teams chat. I want the message to include, dynamically, what is the name of the folder that contains the new file.

Power Users Community thread: Get Name of Folder Containing a New File.

Expressions

My first response was to solve this with an expression. The solution I proposed was using skip, split, sub and length functions to retrieve the third folder of the Folder Path.

This will work great when your folder structure is always the same. However, I realised this is very unlikely in most cases.

Surely there must be an easier way of retrieving a parent folder?!

FieldValuesAsText in REST API

Turns out, yes there is.

I started exploring the REST API and had a look at the FieldValuesAsText property. I have used it before and it sometimes just shows additional properties compared to the regular item. And this was the case again. The FieldValuesAsText contains a string value for ParentUniqueId.

The ParentUniqueId can be used in a GetItemByUniqueId method to retrieve the parent item. With a $select query parameter you can only retrieve the FileLeafRef, which should be the folder name.

Flow setup

In this example the parent folder is retrieved from a newly created file and the name is shared via a Teams message.

getparentfolder

1. Add a When a file is created (properties only) trigger action.

whenafileiscreated

a. Select your Site Address
b. Select your Library Name.

2. Add a initialize variable action (optional).
This variable is used in the GetByTitle method in the REST API requests. This variables is optional because you can also add the list name value directly in the URI fields.

documentlibraryname_variable

a. Provide a Name, I used DocumentLibraryName
b. Select String as type
c. Provide a value. It should be the same name as in 1b.

3. Add a Send an HTTP request to SharePoint action.
This action will retrieve the ParentUniqueId property value of the item.

parentuniqueid

a. Select your Site Address
b. Use the GET method
c. Use the URI from the codesnippet below

d. Use the Headers from the codesnippet below

4. Add a second Send an HTTP request to SharePoint action.
This action will retrieve the FileLeafRef property value of the parent item. A slice function is used to remove the curly bracket characters.

getitembyuniqueid_fileleafref

a. Select your Site Address
b. Use the GET method
c. Use the URI from the codesnippet below

d. Use the Headers from the codesnippet below

5. Add a Get my profile (V2) action (optional).

getmyprofile

6. Add a Post message in a chat or channel action.

postmessage_parentfoldername

a. Select Post as Flow bot
b. Select Post in Chat with Flow bot
c. Use the Mail dynamic content property from the Get my profile action in the Recipient field or just lookup a person in case you are not using the Get my profile action from step 5.
d. Use the following expression for the Message field.

That is it for the setup of this example.

Happy testing!

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.