Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sometimes someone might screw up you sharepoint user related info by deleting information on the user profile service... This script might help you out restoring the user information integrity on your site...
function Restore-SPUserInfo{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, HelpMessage='data container url')]
[Alias('webUrl')]
[string]$url,
[Parameter(Mandatory=$true, HelpMessage='library template type id')]
[Alias('libraryTemplateType')]
[int]$templateType,
[Parameter(Mandatory=$false, HelpMessage='parent content type id for the data you want to process')]
[Alias('ParentContentTypeId')]
[string]$contentTypeId = '0x012000BA09C4DAA7814877BB4C676F16DD8C69'
)
process{
$snapin = Get-PSSnapin | Where-Object { $_.Name -eq 'Microsoft.SharePoint.Powershell' }
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
Write-Host -ForegroundColor Yellow "Starting user information restore proccess for " $web.Title " ( " $web.Url " )"
$web = Get-SPWeb $url
$libraryCollection = $web.Lists
$filteredLibraryCollection = $libraryCollection | Where-Object { $PSItem.BaseTemplate -eq $templateType }
$listItemCollection = $null
$itemCount = 0
$itemIndex = 0
$spuserfields = $null
$currentSPUserFieldValue = $null
$currentSPItem = $null
$spUser = $null
Write-Host "found " $filteredLibraryCollection.Length " lists to fix"
$filteredLibraryCollection | ForEach-Object {
Write-Host -ForegroundColor Green "Proccessing list " $_.Title
$listItemCollection = $_.Items | Where-Object { $_.ContentTypeId.ToString().StartsWith($contentTypeId) }
$itemCount = $listItemCollection.Length
$itemIndex = 0
Write-Host -ForegroundColor Red $itemCount " items ready to be fixed"
$listItemCollection | ForEach-Object {
$itemIndex++
$currentSPItem = $_
Write-Host "Proccessing item " $_.Title " ( " $itemIndex " from " $itemCount " )"
$spuserfields = $_.Fields | Where-Object { $_.Type -eq 'User' }
Write-Host "This item has " $spuserfields.Count " user fields"
$spuserfields | ForEach-Object{
$currentSPUserFieldValue = New-Object Microsoft.SharePoint.SPFieldLookupValue($currentSPItem[$_.StaticName])
if($currentSPUserFieldValue.LookupValue -ne $null){
$spUser = $web.SiteUsers | Where-Object { $_.Name -eq $currentSPUserFieldValue.LookupValue }
if($spUser -eq $null){
Write-Host -ForegroundColor Red "Cannot find a user " $currentSPUserFieldValue.LookupValue " on the user collection"
}
else{
Write-Host -ForegroundColor Green "Updating value for field "$_.StaticName " changing old user id, from " $currentSPUserFieldValue.LookupId " to " $spUser.Id
$currentSPItem[$_.StaticName] = $spUser.Id
}
}
}
$currentSPItem.SystemUpdate()
Write-Host -ForegroundColor Yellow "all user fields have been updated for listitem " $currentSPItem.Title
}
}
}
}
Restore-SPUserInfo -url "http://portal.kdev.local/sites/docs" -templateType 10008 -contentTypeId "0x0120D520002D98229591BE4FE5801600ADF9359CC800F0832B33D4BD484FBBB03C25CD99400F"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment