Find and Replace User ID in SharePoint InfoPath Forms using PowerShell
Requirement: Replace User IDs in InfoPath Forms.
A little background: A particular user’s User account changed in the Active directory, and in SharePoint, we ran the Move-SPUser cmdlet to accommodate the user’s SAM account name change. However, many InfoPath form libraries have numerous InfoPath forms with an old user ID. Of course, Move-SPUser does not affect InfoPath Forms!
PowerShell script to search and replace user id in InfoPath XML Forms:
This PowerShell script iterates through all InfoPath forms in a given site collections, scans, and replaces a given user ID from all forms.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Parameters
$SiteCollURL="https://intranet.crescent.com"
#Old and New User IDs - CASE SENSITIVE!
$OldUserID="<pc:AccountId>i:0#.w|Crescent\JohnA</pc:AccountId>"
$NewUserID="<pc:AccountId>i:0#.w|Crescent\JonhAbraham</pc:AccountId>"
#Get all webs in the site collection
$WebsColl = Get-SPSite $SiteCollURL | Get-SPWeb -Limit All
#Iterate through each web
foreach($web in $WebsColl)
{
Write-host "Processing web:"$web.Url
#Get all InfoPath Form Libraries in the web
$InfoPathLibs = $web.lists | where { $_.BaseType -eq "DocumentLibrary" -and $_.BaseTemplate -eq "XMLForm"}
#Loop through each InfoPath form library and Forms (.xml)
Foreach($Library in $InfoPathLibs)
{
Foreach($Item in $Library.Items)
{
# Load the contents of the InfoPath Form
$File = $Item.File
$Data = [System.Text.Encoding]::ASCII.GetString($File.OpenBinary())
#Check if the File has Old ID
if($data.Contains($OldUserID))
{
$Data = $Data.Replace($OldUserID, $NewUserID)
$Data = $Data.Replace("???","") #special characters fix!
$File.SaveBinary([System.Text.Encoding]::ASCII.GetBytes($Data))
Write-host "InfoPath XML File updated at $($web.URL)/$($Item.File.URL)"
}
}
}
}