cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AnotherMSprofil
Level: Powered On

Sharepoint: Copy File when properties updated or modified, file not found error

Hi All.

 

I am trying to build a Flow that will automatically rename the file in a Sharepoint Document Library when some of the file properties change.

The properties may change as a result of a direct edit of Doc Library columns, or by editing the file online (it has a number of Doc Parts linked to respective columns).

For instance, I want my file to be called <Titile>-<Version>-<Status> (retaining the extension) and when the version changes the file gets renamed. Same should happen automatically for the uploaded files.

 

I know that there is no "Rename" or "Move" file action for Sharepoint. Neither can one rename the file using the "Update File Properties" (the most logical way to me). The only solution is then to Copy and then Delete the file.

 

Here the fun begins...

 

There are two mutually types of triggers I have tried:

* "When a file is created or modified" - this one provides all the values and columns, but, for instance, does not provide the File Path that the Copy/Delete actions want.

* "When a file is created or modified in a folder" - this provides all necessary file identifiers for Copy and Delete, but does not provide column values and other metadata, and none of the values it returns are good for "Get File Properties" action ID field (they are simply not displayed as possible inputs there).

 

So, it seems there is not way to use both in the same flow w/o some advanced level woodoo magic such as oDATA requests (maybe).

 

Question 1: Is that right or there is a way to bridge between the two?

 

In trying to find such way I have found that "When a file is created or modified (properties only)" trigger provides an Identifier field. Which, for a file Document.docx in a Document Library called __TestDocLib looks like "TestDocLib%252fDocument.docx"

 

Thus, I am trying to leverage this in my flow (site address removed).

[For some reason I can't insert the picture - the web site is throwing errors]

 

COPY FILE Action:

* Site address: [my site]

* Source file path: [Identifier]

* Destination file path: [Folder Path]/[Name]_[iVersion]

* Overwrite: False

 

iVersion is one of my columns that I have created for testing. All the values are received from the previous step ("When a file is updated of created (properties only" trigger).

 

By the way

Question 3: How do I access file extension when copying the file? String manipulations?

 

However, the flow triggers an error in the Copy File action

The file /sites/<mysite>/TestDocLib%2fDocument.docx does not exist.

 

 

This is very interesting, as if I take this URL (add the hostname prefix, of course, we are using SP Online) and paste it in by browser - I can download the file!

 

Question 3: What am I doing wrong?

Additional error details below, as well as a couple more related questions

 

Question 4: How to make Flocw check for trigger more often than once/5min or how to make SP Online send a notification to Flow to execute a flow?

 

!Many thanks for any guidance that will help me progress!

 

COPY Files - Inputs

* Site address:https://[company].sharepoint.com/sites/[mysite]

* Source File Path: TestDocLib%252fDocument.docx

* Destination File Path: TestDocLib//Document-1.0.0-001

* Overwrite: False

 

COPY Files - Outputs

* Status code: 404

* Headers: (nothing insteresting here)

* Body

"status": 404,

"message": "The file /sites/[mysite]/TestDocLib%2fDocument.docx does not exist.\r\nclientRequestId: [SNIP]",

"source": "https://[mydomain].sharepoint.com/sites/[mysite]/_api/web/GetFileByServerRelativePath(decodedUrl=@p)/Etag?@p='%2fsites%2[mysite with URLencode]%2fTestDocLib%252fDocument.docx'",

"errors": [ "-2130575338", "Microsoft.SharePoint.SPException" ]

1 ACCEPTED SOLUTION

Accepted Solutions
AnotherMSprofil
Level: Powered On

Re: Sharepoint: Copy File when properties updated or modified, file not found error

Here's the solution I'm currently using. I do not find it optimal, but it works.

Since I still can't insert pictures w/o triggering post submission bugs, plain text only.

 

[Trigger - Sharepoint] When a File is Created or Modified (properties only)

* Site address: my site

* Library name: my library

 

[Action - Variables] Initialize Variable

* Name: FileExtIndex

* Type: Integer

* Value: lastIndexOf(triggerBody()?['{Identifier}'],'.')

// basically, we are looking for the last "." in the {Identifier} that we have received from the Trigger. Will not work with files like .tag.gz, but I do not expect to see them there. I do expect files with dots in name such as "Document v2.2.docx"

 

[Action - Variables] Initialize Variable 2

* Name: FileExtLength

* Type: Integer

* Value: sub(length(triggerBody()?['{Identifier}']),variables('FileExtIndex'))

// basically the length of the extension substring, including the dot itself. We need this for the next step.

 

[Action - Variables] Initialize Variable 3

* Name: FileExt

* Type: String

* Value: substring(triggerBody()?['{Identifier}'],variables('FileExtIndex'),variables('FileExtLength'))

// Basically, "extract a subsring of {Identifier} {FileExtIndex}, beginning with {FileExtIndex} for {FileExtLength} characters" gives us the file extension with a dot. I expected Flow to implement all sorts of string functions such as "left, right" and of course the Regexps. Shame..

 

[Action - Sharepoint] Copy File

* Site address: <my site>

* Source file path: {Folder Path}{Name}{FileExt}

// note that there are no '/' or '.' - they are already included in the variables and neither Flow nor Sharepoint REST APIs are nice enough to sort out the duplicates for you.

* Destination file path: {Folder Path}{Name}-{iVersion}{FileExt}

// I am adding the value of the {iVersion} column, which I created for testing, to the file name

* Overwrite: no

// I do not expect such file to exist and would rather have the flow fail than overwrite it. Pity there is no "File Exists" condition in Sharepoint. One would have to use "Get Files" with oDATA filter, but I haven't yet learned oDATA.

 

[Action - Sharepoint] Delete File

* Site address: <my site>

* File identifier: {Identifier}

// Interestingly enough, here Sharepoint doesn't care about URLencoding in the {Identifier}, but in Copy File this causes an error.

 

 

This is it!

 Can it be improved?

View solution in original post

2 REPLIES 2
AnotherMSprofil
Level: Powered On

Re: Sharepoint: Copy File when properties updated or modified, file not found error

After some more playing around I can see that the issue is because of the double URLencoding

 

If I take the URL from the error message and directly paste into browser I get the expected error

https://[mydomain].sharepoint.com/sites/[mysite]/_api/web/GetFileByServerRelativePath(decodedUrl=@p)/Etag?@p='%2fsites%2[mysite with URLencode]%2fTestDocLib%252fDocument.docx

 

However, if I replace the highligted part (%252f) with '/' - everything works!

 

https://[mydomain].sharepoint.com/sites/[mysite]/_api/web/GetFileByServerRelativePath(decodedUrl=@p)/Etag?@p='%2fsites%2[mysite with URLencode]%2fTestDocLib/Document.docx

 

 

In the end I were able to get the solution working. However, I had to create a bunch of variables to disassemble and reassemble the Identifier as follows: [Folder Path][Name][FileExt], where [FileExt] is my own string variable that extracts the extension from [Identifier] in a series of string and math operations.

 

I thought the purpose of MS Flow is to free people flow all this nonsense to focus on business logic?!

Anyway, I will post the solution in a separate reply in case someone needs it.

 

AnotherMSprofil
Level: Powered On

Re: Sharepoint: Copy File when properties updated or modified, file not found error

Here's the solution I'm currently using. I do not find it optimal, but it works.

Since I still can't insert pictures w/o triggering post submission bugs, plain text only.

 

[Trigger - Sharepoint] When a File is Created or Modified (properties only)

* Site address: my site

* Library name: my library

 

[Action - Variables] Initialize Variable

* Name: FileExtIndex

* Type: Integer

* Value: lastIndexOf(triggerBody()?['{Identifier}'],'.')

// basically, we are looking for the last "." in the {Identifier} that we have received from the Trigger. Will not work with files like .tag.gz, but I do not expect to see them there. I do expect files with dots in name such as "Document v2.2.docx"

 

[Action - Variables] Initialize Variable 2

* Name: FileExtLength

* Type: Integer

* Value: sub(length(triggerBody()?['{Identifier}']),variables('FileExtIndex'))

// basically the length of the extension substring, including the dot itself. We need this for the next step.

 

[Action - Variables] Initialize Variable 3

* Name: FileExt

* Type: String

* Value: substring(triggerBody()?['{Identifier}'],variables('FileExtIndex'),variables('FileExtLength'))

// Basically, "extract a subsring of {Identifier} {FileExtIndex}, beginning with {FileExtIndex} for {FileExtLength} characters" gives us the file extension with a dot. I expected Flow to implement all sorts of string functions such as "left, right" and of course the Regexps. Shame..

 

[Action - Sharepoint] Copy File

* Site address: <my site>

* Source file path: {Folder Path}{Name}{FileExt}

// note that there are no '/' or '.' - they are already included in the variables and neither Flow nor Sharepoint REST APIs are nice enough to sort out the duplicates for you.

* Destination file path: {Folder Path}{Name}-{iVersion}{FileExt}

// I am adding the value of the {iVersion} column, which I created for testing, to the file name

* Overwrite: no

// I do not expect such file to exist and would rather have the flow fail than overwrite it. Pity there is no "File Exists" condition in Sharepoint. One would have to use "Get Files" with oDATA filter, but I haven't yet learned oDATA.

 

[Action - Sharepoint] Delete File

* Site address: <my site>

* File identifier: {Identifier}

// Interestingly enough, here Sharepoint doesn't care about URLencoding in the {Identifier}, but in Copy File this causes an error.

 

 

This is it!

 Can it be improved?

View solution in original post

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 225 members 5,116 guests
Please welcome our newest community members: