I am using SP2013 and trying to update the “Author” and “Editor” properties of a folder in a document library. When I run my script it appears that it updates the fields correctly, however, when I check the values again they are not actually modified. I am not sure why … Here is my existing script.
$web = Get-SPWeb “http://mytestbox/sites/tech/”
$site=$web
$list = $web.Lists[“Reports”]
foreach ($item in $list.Folders)
{
$itemauthor = $item[“Author”]
$itemeditor = $item[“Editor”]
$itemmod = $item.Properties.vti_modifiedby
write-host “Author is: ” $itemauthor
write-host “Editor is: ” $itemeditor
write-host “Modified By is: ” $itemmod
if($itemauthor -eq “1;#domainxyz\doveal”)
{
$itemauthor = “1;#sharepoint\system”
Write-Host “The folder author is ” $itemauthor
$item.Update()
Write-Host “The folder author is updated”
}
if($itemeditor -eq “1;#domainxyz\doveal”)
{
$itemeditor = “1;#sharepoint\system”
Write-Host “The folder editor is ” $itemeditor
$item.Update()
Write-Host “The folder editor is updated”
}
if($itemmod -eq “i:0#.w|domainxyz\doveal”)
{
$itemmod = “i:0#.w|sharepoint\system”
Write-Host “The folder modified by is ” $itemmod
$item.Update()
Write-Host “The folder modified by is updated”
}
write-host “Author is: ” $itemauthor
write-host “Editor is: ” $itemeditor
write-host “Modified By is: ” $itemmod
}
$site.Update()
$site.Dispose()
Thanks for your appreciation first.
EnsureUser keyword ensure that user must be available in SharePoint User Information list first and then provide you SPUser Object.
Get-SPUser -identity “SHAREPOINT\system” -web $web not able to retrieve the user from SharePoint.
Ajeet,
Thank you. This worked perfectly!
Can you explain why your code worked where my code was broken?
#$user = Get-SPUser -identity “SHAREPOINT\system” -web $web
vs
[Microsoft.SharePoint.SPUser]$user = $web.EnsureUser(“SHAREPOINT\system”)
Hi Alex
use this code:
$inputUrl = Read-Host “Enter the site URL”
$libName = Read-Host “Enter list or library name”
$web = Get-SPWeb -Identity $inputUrl
$list = $web.Lists[“$libName”]
[Microsoft.SharePoint.SPUser]$user = $web.EnsureUser(“<LoginName of the User>”)
#$user = Get-SPUser -identity “q3tech\sp_farm” -web $web
foreach ($item in $list.folders)
{
$item[“Editor”] = $user
$item.Update()
}
$web.Dispose
Here I have used Jason code with only one change.
I have replaced
$user = Get-SPUser -identity “SHAREPOINT\system” -web “<someSPWeb>”
with
[Microsoft.SharePoint.SPUser]$user = $web.EnsureUser(“SHAREPOINT\system”)
and it works fine now
maybe SharePoint 2013 is looking for more than one parameter to .Update(). If it doesn’t see both, it throws this error. Maybe try updating both Author and Editor and see what happens. This behavior isn’t present in my 2010 version (14.0.7113.5000)