SharePoint error User cannot be found

When you try to get the Author or ModidfiedBy properties from a document in sharepoint using the SharePoint API Library you get an exception error “User cannot be found”
The reason for this, is that the properties vti_author and/or vti_modifiedby are pointing to an user that no longer exists.

When you change these properties to a user that exists you no longer get this error
With this powershell code you can get these properties and change them.

$spsite = "http://somerootsite/somesubsite"
$FileName = "somedoclib/somefolder/somefile"

$spweb = Get-SPWeb $spsite
$spfile = $spWeb.GetFile($FileName).item
$CreatedDate = $spfile["Created"]
$ModifiedDate = $spfile["Modified"]
$Author = $spfile["Author"]
$Editor = $spfile["Editor"]

#You'll see an ID followed by ;# followed by the SharePoint user displayname
#The problem is not this property, but the following property!

$spfile.properties["vti_author"]
$spfile.properties["vti_modifiedby"]

#This user is the AD user i:0#.w|<domain>\<username>
#If this user does not exist you'll get the error
#Change it with:

$spfile.properties["vti_author"]= "i:0#.w|correctdomain\correctusername"
$spfile.properties["vti_modifiedby"] = "i:0#.w|correctdomain\correctusername"
$spfile["Created"] = $CreatedDate
$spfile["Modified"] = $ModifiedDate
$spfile.UpdateOverwriteVersion()

Leave a Reply

Your email address will not be published.