none
Powershell to replace text in infopath XML files RRS feed

  • Question

  • Please share the powershell script which will replace a set of text with a new set of text from all InfoPath libraries xml files for all SharePoint sites in 2010 and 2013

    Santosh sethi

    Thursday, June 30, 2016 10:13 AM

Answers

  • Hi Santosh,

    Here is an demo for your reference:

    $Path = "\\spsite\sites\subsite\Library\"
    $Files = Get-ChildItem $Path | where {$_.extension -eq ".xml"}
    foreach($file in $Files){
        $xml = New-Object XML
        $xml.PreserveWhitespace = $true
        $xml.Load($file.fullname)
        $xml.myFields.PhoneNumber = "234-5678"//update your fields on the form
        $xml.Save($file.fullname)
    }
    

    Best Regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, July 1, 2016 6:59 AM
    Moderator

All replies

  • Hi

    this is the code to update a XML file

    http://stackoverflow.com/questions/16428559/powershell-script-to-update-xml-file-content

    You need to check for XML files in a specific path

    Get-ChildItem -Path C:\path -Filter *.xml -Recurse

    and update them


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Thursday, June 30, 2016 12:18 PM
  • Hi Santosh,

    Here is an demo for your reference:

    $Path = "\\spsite\sites\subsite\Library\"
    $Files = Get-ChildItem $Path | where {$_.extension -eq ".xml"}
    foreach($file in $Files){
        $xml = New-Object XML
        $xml.PreserveWhitespace = $true
        $xml.Load($file.fullname)
        $xml.myFields.PhoneNumber = "234-5678"//update your fields on the form
        $xml.Save($file.fullname)
    }
    

    Best Regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, July 1, 2016 6:59 AM
    Moderator
  • Hi Santosh,

    Please use below code for replacing text in infoPath XML files,

    Here is resolved thread,

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/44aa9b9b-6185-4860-aea3-86597872c43e/powershell-search-multiple-files-for-a-text-string-and-replace-with-corresponding-new-text-values?forum=ITCG

    Please use below code fro your reference for replacing text in infoPath XML files,

    C# :

    StreamReader sr = new StreamReader(filename);
    String file = sr.ReadToEnd();
    sr.Close();
    file = file.Replace(oldtext, newtext);
    StreamWriter sw = new StreamWriter(filename);
    sw.Write(file);
    // Close StreamWriter
    sw.Close();

    PowerShell Scripting : 


    $csvvalue='new value here'
    
    [xml]$xml=Get-Content file.xml
    $node=$xml.SelectSingleNode('//destPath')
    $node.'#text'=$csvvalue
    $xml.Save('file.xml')

    Hope this help.

    Let me know in case any further information required.

    Marked as Answer, if it helps you.

    Vote if you find useful.

    Regards,


    Amjad Khan




    Friday, July 1, 2016 7:09 AM