1

The Author field (created by) is not updating via the powershell script. The Editor and created and modified date fields are all updating as expected. i am pulling the list data off of a CSV. I get no errors when attempting to write to the Author field, even when I manually add junk data into the script replacing the getUser call. The getUser call is a separate function, which works fine with the Editor field,

Any ideas?

Here's a snippet of script:

foreach($row in $tblData){
  item.File.Name.Length -gt 0){
    if($row."FileName" -eq $item.File.Name){
      write-host 'file ' $item.File.Name
      $fileCount++
      try{
          write-host 'updating info for ' $item.File.Name
          $item["Author"] = getUser($row."CreatedBy")
          $item["Editor"] = getUser($row."ModifiedBy")
          $item["Created"] = [DateTime]$row."CreatedAt"
          $item["Modified"] = [DateTime]$row."ModifiedAt"
          $item.Update()
          $Context.Load($item)
          $Context.ExecuteQuery()
      }
      catch [System.Exception] 
      { 
         Write-Host -ForegroundColor Red $_.Exception.ToString()    
      }   
   }
}

EDIT: Thanks guys. Some more information:

I am working on this as part of a migration. The script appears to set the Author field at first pass with a brand new document library. However, the Author field is then reset to me after a minute or so. I had moved the documents over with the SP Sync functionality - copying/pasting the documents locally and syncing with the new SP location, then turning off SP offline mode for the site collection. It makes a bit of sense that the fields would reset to the synced documents, but it only ever happens on the Author field - all other fields work as expected.

1 Answer 1

0

You can achieve this using below script , Please make sure that correct user object is assigned to the Author field:

if its works perfect with Editor filed then the user object assigned wrongly to the Author field , try adding a hard coded user object as below and check the same.

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0 
#specify new user - login name 
$newuser = "Domain\user" 
#specify the web URL 
$site = get-SPWeb http://vm353/pwa/ProjectBICenter 
#specify the list name 
$list = $site.Lists["Test"] 
$listitems = $list.Items 
$user = get-SPuser -Web $site -Identity $newuser 
#loop through each list item and update 
foreach ($listitem in $listitems) 
{ 

    $listitem["Author"] = $user 
    $listitem["Editor"] = $user        
    $listitem.Update() 
    write-host $listitem["Name"] "has been updated. The author and editor has been set to $user" 

} 
$site.Update(); 
$site.Dispose();
2
  • 1
    your script will not work because environment is SharePoint Online Dec 11, 2016 at 13:12
  • Ok , will have a look over it
    – Esaki
    Dec 12, 2016 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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