SOLVED

Power Automate Sharepoint file Copy not keeping the created by

Brass Contributor

Like many I'm trying to move my SharePoint Designer Workflows to Power Automate. I ran into a problem with copying a file from one SharePoint library to another. I use 2 libraries, one where the users can add a file, then the file is copied to an archive library where they can not make any changes. Currently I'm using a workflow in impersonation mode where when a file is changed it copies the file to the archive library overwriting what is there. I've mimicked this in Power Automate using a created / modified trigger which works but it puts me as the created by when I would like the actual user who created it. Is there anyway around this?

Thanks

15 Replies
Created by is always whatever account added the file, this cannot be changed, modified should be retained however. The only exception I've seen is bulk migration tools utilizing the bulk import methods.

@ChrisWebbTech 

Thank you for your response. With the workflow and the impersonation step, the copied file would keep the created by and the modified would change to the impersonation account.

Now with flow it is changing both the created by and the modified by to the account that created the flow loosing the meta data I need.

Is there anyway to change this?

best response confirmed by Sean Kuchle (Brass Contributor)
Solution

@Sean Kuchle 

Ok, so I can't find anywhere that says this is possible or how to do it, but I just figured it out, at least it works for me going cross site. So the trick is to utilize the built in move to command via REST API. There is a hidden little property on it that acts like a move but retains the source since copy doesn't retain metadata but move to command will. So this seems to work, here is a Screen of my test flow. I'll also paste the Body text down below so you can copy and paste, but it's actually pretty simple once you figure out all this undocumented stuff like the Conflictname behavior which I just guessed at to find the right number to override the name. You can also change that to a 2 if you want to generate a new file for each update instead, or 0 to do nothing if one exists. Anyway hope this works for you, not sure how it'll work with files already open for editing but hopefully it still copies and doesn't error when locked for editing otherwise I don't think it'll be possible. You can get status of the job etc. to do retries if that's the case but that's outside my time to spend on this limitations :).

 

copy file.PNG

API: _api/site/CreateCopyJobs

Body: 

{
"exportObjectUris":[
"Link to Item from Get File Properties Step goes here"
],
"destinationUri":"https://Your_Tenant.sharepoint.com/sites/YourSite/YourLibrary_NoSlash_at_end",
"options":{
"IgnoreVersionHistory":true,
"IsMoveMode":true,
"MoveButKeepSource":true,
"NameConflictBehavior":1
}
}

Ops, sorry, also the First trigger would obviously your "When created or modified" instead to automate it, I had for selected for testing purposes.
Well, I don't understand why, but this just all the sudden stopped working and says bad gateway, but it was working just fine up till I messaged you. Not sure if my testing caused an issue or not, but give it a go and see if it works for you. I'm going to give this some time cause I think something is up with the API or something right now cause all I did was change my views to not show folders and tried to run one and it stopped working after that even after changing the views back, so something in that transition upset the API on my end so don't do that with your view ;).

@ChrisWebbTech 

Thank you so much for your help and that seems like a neat hack but I am getting the "BadGateway" errors as well. It says 2 retries occurred.

Just to make sure I didn't miss anything here is the flow

Example-9-2-2020.png

Yeah I'm literally getting the same thing, it just stopped working out of the blue, annoying cause I had figured this out and now it just quit for no reason it seems.
Wow figured it out, I don't know what changed with my columns by enabling that view, but add this in.
"NameConflictBehavior":1,
"AllowSchemaMismatch":true

At the end, make sure you put that comma after the 1 in first existing line, then add the allowschemamismatch, this solved it for me and it's working again.

@ChrisWebbTech 

That worked great! Thank you so much for your help!

I'm just curious is there anywhere I can go to learn more about these Api calls?

@Sean Kuchle 

Sweet! If you want to pull across version history you can by changing that flag false. Now I don't know if doing this resets your versions either, that could be a side effect, so you might need to test that, and if someone has a file open etc. Hopefully it just leaves existing file and copies one over using move method.

 

Anyway, I just googled stuff up cause I've had to use REST api calls to rename a file and thought there would be a copy one, which there is but it doesn't retain the meta either. Then I stumped across this docs article for the CreateCopyJobs and a blog about using it for copying files, but I read over the docs and happen to notice that property and came up with the idea of using that, built it and tested and figured out it does retain that information like a move!

 

@ChrisWebbTechI had a wired issue happen today I was hoping you might have some insight on. I'm using the solution you came up in 3 power automates. 1 for what ever reason has stopped working although there are no errors in power automate and from that prospective it seems to have completed successfully but the file is not copied. More specifically it has 1/2 stopped working. I have it copy the file when it is first created then copy it when there are changes. It does copy it when it is new but will not copy the changed one, again there are no error in the flow.

I've found several articles about how to look into the rest api but I can't seem to get them working, I'm not sure I've found the right directions.

Any ideas?

Thanks

Sean

 

@ChrisWebbTech  Never mind I was able to figure out the problem. The particular library had a few extra columns. I recreated them in the library that I am coping the files to and it start working. It is weird though considering the other 2 libraries I'm coping from have extra columns that are not in the new library and it works. But maybe it is being extra picky for this library.

Thanks

Sean

@ChrisWebbTechI just had to chime in that this still works and exactly what I needed. I created a flow that copied a file to a teams location, but my name was on the "modified by field". Swapped your setup within my if condition and it works perfectly. Thanks again!

@ChrisWebbTechI have a question. Is there another number for NameConflict behavior? So, I see that 1 is replace and 2 is copy, but what if I want to ignore if a file with the same name is already there? Is there an option for that? 

Hi @ChrisWebbTech i hope you're doing great! I tried to use your template but unfortunately it doesn't work with the newer actions. May I ask you to make a new version for your template since I'm no Power Automate specialist? Thank you!

1 best response

Accepted Solutions
best response confirmed by Sean Kuchle (Brass Contributor)
Solution

@Sean Kuchle 

Ok, so I can't find anywhere that says this is possible or how to do it, but I just figured it out, at least it works for me going cross site. So the trick is to utilize the built in move to command via REST API. There is a hidden little property on it that acts like a move but retains the source since copy doesn't retain metadata but move to command will. So this seems to work, here is a Screen of my test flow. I'll also paste the Body text down below so you can copy and paste, but it's actually pretty simple once you figure out all this undocumented stuff like the Conflictname behavior which I just guessed at to find the right number to override the name. You can also change that to a 2 if you want to generate a new file for each update instead, or 0 to do nothing if one exists. Anyway hope this works for you, not sure how it'll work with files already open for editing but hopefully it still copies and doesn't error when locked for editing otherwise I don't think it'll be possible. You can get status of the job etc. to do retries if that's the case but that's outside my time to spend on this limitations :).

 

copy file.PNG

API: _api/site/CreateCopyJobs

Body: 

{
"exportObjectUris":[
"Link to Item from Get File Properties Step goes here"
],
"destinationUri":"https://Your_Tenant.sharepoint.com/sites/YourSite/YourLibrary_NoSlash_at_end",
"options":{
"IgnoreVersionHistory":true,
"IsMoveMode":true,
"MoveButKeepSource":true,
"NameConflictBehavior":1
}
}

View solution in original post