Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Find and Replace User ID in SharePoint InfoPath Forms using PowerShell

$
0
0
Requirement: Replace User IDs in InfoPath Forms.

Little background: A particular user's User account changed in Active directory and in SharePoint we ran Move-SPUser cmdlet to accommodate the user's SAM account name change. However, There are many InfoPath form libraries with bunch of InfoPath forms in it - with old user id. Of course, Move-SPUser has no effect on InfoPath Forms!

PowerShell script to search and replace user id in InfoPath XML Forms:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteCollURL="http://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 fix!
$File.SaveBinary([System.Text.Encoding]::ASCII.GetBytes($Data))

Write-host "InfoPath XML File updated at $($web.URL)/$($Item.File.URL)"
}
}
}
}

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>