0

I have an enterprise wiki site collection inside my sharepoint on-premises farm 2013. now i have imported some wiki pages from different farm. and since the users who created the original wiki pages do not exists inside our farm, so the createdby and the modifiedby fields for the wiki pages have been set to "System Account" after doing the import operation for the Pages list. now i want to modify the CreatedBy & ModifiedBy fields for all my enterprise wiki pages , so i tried this power-shell script where i used similar appraoch mentioned in this link:-

#specify new user - login name 
$newuser = $web.EnsureUser("ad-***\****.***") 
#specify the web URL 
$site = get-SPWeb http://****/kb/WikiSite/ 
#specify the list name 
$list = $site.Lists["Pages"] 
$listitems = $list.Items 
$user = get-SPuser -Web $site -Identity $newuser 
#loop through each list item and update 
$Site.AllowUnsafeUpdates = $true
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()

but i got this error:-

Exception calling "Update" with "0" argument(s): "Invalid data has been used to update the list item. The field you are trying to update may be read only." At line:5 char:9 + $listitem.Update() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SPException

now i tried to use $listitem.UpdateOverwriteVersion() instead of $listitem.Update(),, but did not fix the problem.. so can anyone adivce on this problem please?

  • 1
    Did you try $listitem.SystemUpdate() – Nadeem Yousuf Nov 9 '17 at 3:10
  • @NadeemYousuf yes same error – john Gu Nov 9 '17 at 13:46
1

You don't need this line:

$user = get-SPuser -Web $site -Identity $newuser 

Instead, directly pass the $newuser to the list items.

So, your overall code would be as below:

#specify new user - login name 
$newuser = $web.EnsureUser("ad-***\****.***") 
#specify the web URL 
$site = get-SPWeb http://****/kb/WikiSite/ 
#specify the list name 
$list = $site.Lists["Pages"] 
$listitems = $list.Items 

#loop through each list item and update 
$Site.AllowUnsafeUpdates = $true
foreach ($listitem in $listitems) 
{ 

    $listitem["Author"] = $newuser 
    $listitem["Editor"] = $newuser     
    $listitem.Update()

    write-host $listitem["Name"] "has been updated. The author and editor has been set to $newuser" 
} 
$Site.AllowUnsafeUpdates = $false
$site.Update() 
$site.Dispose()

Or you can also use the SPFieldUserValue as below:

$newuser = $web.EnsureUser("ad-***\****.***") 
$UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $newuser.ID, $newuser.LoginName)
#specify the web URL 
$site = get-SPWeb http://****/kb/WikiSite/ 
#specify the list name 
$list = $site.Lists["Pages"] 
$listitems = $list.Items 

foreach ($listitem in $listitems) 
{ 

    $listitem["Author"] = $UserFieldValue 
    $listitem["Editor"] = $UserFieldValue
    $listitem.Update()
    write-host $listitem["Name"] "has been updated. The author and editor has been set to $newuser" 
} 
$Site.AllowUnsafeUpdates = $false
$site.Update() 
$site.Dispose()
  • now for a reason or another when i used $listitem.SystemUpdate() the CreatedBy got updated,,, but the ModifiedBy did not changed... now if i use $listitem.Update() the ModifiedBy got updated,,, but also the modified date got updated.. so is there a specific reason why the ModifiedBy field did not get updated if i am using $listitem.SystemUpdate() ?? – john Gu Nov 9 '17 at 13:14
  • 1
    SystemUpdate does not change the Modified or Modified By fields. Check this link – Gautam Sheth Nov 9 '17 at 14:31
  • so in this case it is mandatory to user $listitem.Update() which will create a new version for the page + modify the last modified date .. unlike $listitem.SystemUpdate() which will not created a new version and will not modify the modified date..is this correct?? – john Gu Nov 9 '17 at 15:23
  • you can try the $listitem.UpdateOverwriteVersion() instead of $listitem.Update() as well which will not update version of the item – Gautam Sheth Nov 9 '17 at 16:11

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.