I've copied some files from one library to another with Powersehll.
Now I'm trying to update Author and Editor. Unfortunately, the author remains the user with wich I copied the item and the editor results empty.
Here is the code I have used:
[SPListItem]$spfilein = $spwebin.GetListItem($urltofilein)
[SPListItem] $spfileout = $spwebout.GetListItem($urltofileout)
[SPUser] $userin = $spfilein["Author"]
[SPFieldUserValue] $val = New-Object Microsoft.SharePoint.SPFieldUserValue($spwebin, $userin.ID, $userin.Name);
$spfileout["Author"] = $val;
$spfileout["Editor"] = $spfilein["Editor"];
$spfileout.UpdateOverwriteVersion(); # $spfileout.Update() same result
$spwebout.Update();
I've also tried:
$spfileout["Author"] = $spfilein["Author"];
$spfileout["Editor"] = $spfilein["Editor"];
$spfileout.UpdateOverwriteVersion();
$spwebout.Update();
Do you know what is wrong?
EDIT: Even tried the code below. Author remain empty.
$spfileout["Author"] = $spfilein["Author"];
$spfileout["Editor"] = $spfilein["Editor"];
$spfileout.Properties["vti_author"] = $userin.LoginName
$spfileout.Properties["vti_modifiedby"] = $userin.LoginName
$spfileout.Update();
EDIT2 One problem I had was that the user on $spwebout had never acessed the site and that caused the empty author,editor. Anyway, even so I cannot save the author afeter updating the list item author and edittor become the administrative user I am using for the update.
I've tride even impersonating as suggested by one of the answerer but no luck as author and editor become the script runner.
$s = get-impersonatedsite -url:"http://myurl/mysitecollection" -user: $userAuthor.LoginName
[SPWeb] $spwebout = $s.OpenWeb()
[SPListItem] $spfileout = $spwebout.GetListItem($urltomyfile)
$spfileout["Author"] = $userAuthor;
$spfileout["Editor"] = $userAuthor;
$spfileout.Update()