Monday, January 28, 2019

Powershell to replace string in infopath in SharePoint Document library

So here is a simple/dirty script to update the files in your document library.  this particular script is for infopath documents with the template pointing to the wrong url after a migration

Add-PSSnapin *sharepoint* -EA SilentlyContinue

$web = Get-SpWeb https://url

#find your list (your call on what method to use
$list = $web.Lists[0] 

$oldstring = "https://url/libraryname/Forms/template.xsn"

$newstring = "https://url/Newlibraryname/Forms/template.xsn"

foreach($i in $list.Items)
{
  if($i.Title.Contains('.xml'))
  {
     $data = [System.Text.Encoding]::ASCII.GetString($i.File.OpenBinary())
     if($data.Contains($oldstring)
     {
         $data = $data.Replace($oldstring, $newstring)
       
          $i.File.SaveBinary([System.Text.Encoding]::ASCII.GetBytes($data))
      }
  }

}

1 comment:

  1. Thanks, it helped me to work on the same requirement with html file.

    ReplyDelete