Frage

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?

War es hilfreich?

Lösung

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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top