0

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()
1

I think you're using wrong SPWeb to create SPFieldUserValue in this line:

[SPFieldUserValue] $val = New-Object Microsoft.SharePoint.SPFieldUserValue($spwebin, $userin.ID, $userin.Name);

You're using $spwebin to create value assigned to $spfileout.

There should be no problems to update Author field. If user does not exist on target web, you need to use SPWeb.EnsureUser() method.

This should be enough:

$spfilein = $spwebin.GetListItem($urltofilein)
$spfileout = $spwebout.GetListItem($urltofileout)

$inAuthor = [Convert]::ToString($spfilein['Author'])
$inValue = New-Object Microsoft.SharePoint.SPFieldUserValue $webin, $inAuthor

$outUser = $outweb.EnsureUser($inValue.User.LoginName)

$spfileout['Author'] = $outUser
$spfileout.Update()

This line wont work, because users may have different IDs on different webs.

$spfileout["Author"] =  $spfilein["Author"];

This line is not required, because you don't change properties of SPWeb:

$spwebout.Update();

Also specifying variable types in PowerShell looks odd.

1

Created By & Modified By columns are “Person or Goup” type columns. In SharePoint all the lists has these columns by default. You can’t update the values of these columns from UI

You can do it using the below mentioned MSDN link

Updating the “Created By” and “Modified By” Columns in SharePoint lists using POWERSHELL

  • the link you posted propose just what I did. – Gyonder Apr 21 '16 at 11:26
  • Good one.. I was just helping you by giving the answer – Hardik Apr 21 '16 at 11:28
  • is that link helps you as a solution? – Hardik Apr 21 '16 at 12:05
  • 1
    no it did't. It's on method I'd showed in my post. – Gyonder Apr 21 '16 at 12:41
1

This actually worked for me

$modifiedby = $sourceitem["Editor"]

$author = $sourceitem["Author"]

$destinationitem["Author"] = $author

$destinationitem["Editor"] = $modifiedby

0

After setting the Author and Editor field values, do the following:

$spfileout.Properties["vti_author"] = $userin.LoginName
$spfileout.Properties["vti_modifiedby"] = $userin.LoginName

Another note: you don't need to update the SPWeb object at the end of your script if you are just changing SPListItem.

  • I tried SPweb.Update just to see if it could help. Anyway, I've edited the post. Even with your addition the problems is not solved. – Gyonder Apr 21 '16 at 12:39
0

You have to do impersonation with author of each item.

#get a spsite object using the token of a specified user, "impersonating" the object
add-pssnapin microsoft.sharepoint.powershell -ea 0
cls

#use global so everything gets disposed
start-spassignment -global

function get-impersonatedsite (
    [parameter(mandatory=$true)]
    $url,
    [parameter(mandatory=$true)]
    $username
)
{
    try {
        $site = get-spsite $url -ea 0
        if ($site -eq $null) {
            throw "site not found: $url"
        }
        $web = $site.rootweb
        $user = $web.EnsureUser($username)
        if ($user -eq $null) {
            throw "user not found: $username"
        }
        $token = $user.usertoken
        $isite = new-object microsoft.sharepoint.spsite($url, $token)
        return $isite
    }
    catch {
        write-host "error:" $_
    }
 }

try {
    // Pass author login name
    $s = get-impersonatedsite -url:"<<Your Site collection url>>" -user:"<<domain\user name>>"
    #do something as that user with $s
}
catch {
    write-host "error:" $_
}
finally {
    stop-spassignment -global
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.