1

I copied files from a document library to another using powershell. But in new document library, for all files, created by column is the system account. I want to update this value to author of the original file. When I try to edit using powershell is gives me ""Author" is a ReadOnly property." error.

Here is a piece of code. How can I edit the author property?

$sWeb=Get-SPWeb $sourceWebURL
$sList=$sWeb.Lists | ?{$_.Title -eq $sourceLibraryTitle}

$dWeb=Get-SPWeb $destinationWebURL
$dList=$dWeb.Lists | ?{$_.Title -eq $destinationLibraryTitle}

$RootFolder=$sList.RootFolder
$RootItems=$RootFolder.Files
$counter=0
#$RootItems.Count

foreach($RootItem in $RootItems)
{

    Write-Host   $RootItem.Title " copying..."
    $sBytes = $RootItem.OpenBinary() 
    $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true) 
    #Write-Host $RootItem.Author

    $replacedUser=$sWeb.EnsureUser($RootItem.Author)       
    $fieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($sWeb.RootWeb, $replacedUser.ID, $replacedUser.LoginName);
    #Write-Host $fieldUserValue

    $RootItem.Author=$fieldUserValue

    $dFile.Update()   

    $counter=$counter+1
    Write-Host $counter " item copied"    

}
  • I notice that you are taking items from $sList, and updating the items of $sList, with its own value, is it what you are trying.? – Karthik Jaganathan Nov 26 '15 at 8:21
  • you are setting the author of $RootItem -> which belongs to $sList (Source) and then finally without setting any value to the destination item, you are updating the $dFile -> which belongs to $dList. – Karthik Jaganathan Nov 26 '15 at 8:33
1

Try using $dWeb.AllowUnsafeUpdates = "true"; because before updating the read-only fields, we need to enable this, if this doesn't work for you then instead of the $dFile.Update(), try with $dFile.UpdateOverwriteVersion();

Above all the mentioned approaches, please check the logic which you have implemented, I guess the problem is in the logic.

0

I had the same situation .I followed this Powershell command to change the authors for all SharePoint items.

https://social.technet.microsoft.com/Forums/windows/en-US/78526b32-430c-4b62-80ea-87d968e561d2/edit-created-by-column-using-power-shell?forum=sharepointcustomizationlegacy

http://blogs.msdn.com/b/tadd/archive/2008/05/22/updating-the-created-by-and-modified-by-columns-in-sharepoint-lists.aspx

  • Please add details from the lnks to your answer. As it is written now it will be to no help in the future if the link breaks – Robert Lindgren Nov 26 '15 at 14:21
0

Call the following code:

$newItem = $dFile.Item
$newItem["Author"]=$fieldUserValue
$newItem.UpdateOverwriteVersion()   

instead of:

$RootItem.Author=$fieldUserValue
$dFile.Update()

UpdateOverwriteVersion is the only method of SPListItem that allows overwriting of Modified/Modified by (aka Editor)/Created/Created by (aka Author) fields.

Also, call it on the new SPListItem object (i.e. $dFile.Item in your case) and not on the SPFile object.

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.