none
Update xml in Infopath using powershell RRS feed

  • Question

  • Hello Techies,
    I need to update the xml of InfoPath using powershell.

    Need to update the innertext of below 3 nodes :

    <my:URL1>HTTP://SHAREPOINT/sp/abc/DocLib/FR/PCDAMON%2021-529574/2014</my:URL1>
     <my:URL1Read>HTTP://SHAREPOINT/sp/abc/DocLib/FR/PCDAMON%2021-529574/2014</my:URL1Read>
    <my:BaseDocFolder>HTTP://SHAREPOINT/sp/abc/DocLib/FR</my:BaseDocFolder>


    I tried powershell 

    <?xml version="1.0" encoding="UTF-8"?>
    <?mso-infoPathSolution solutionVersion="1.0.0.5376" productVersion="14.0.0" PIVersion="1.0.0.0" href="http://SharePoint/FormLibrary/Forms/template.xsn" name="urn:schemas-microsoft-com:office:infopath:CLAAD-Requests-2-:-myXSD-2010-08-13T08-36-32" ?>
    <?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>  
    
    <my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:s0="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-08-13T08:36:32" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="fr">  
    <my:FileAttachments>
        <my:FileAttach1 xsi:nil="true">
        </my:FileAttach1>
        <my:FileAttach2 xsi:nil="true">
        </my:FileAttach2>
        <my:FileAttach3 xsi:nil="true">
        </my:FileAttach3>
        <my:URL1>HTTP://SHAREPOINT/sp/abc/DocLib/FR/PCDAMON%2021-529574/2014</my:URL1>
        <my:URL2>
        </my:URL2>
        <my:URL3>
        </my:URL3>
        <my:URL1Read>HTTP://SHAREPOINT/sp/abc/DocLib/FR/PCDAMON%2021-529574/2014</my:URL1Read>
        <my:URL2Read>
        </my:URL2Read>
        <my:URL3Read>
        </my:URL3Read>
        <my:BaseDocFolder>HTTP://SHAREPOINT/sp/abc/DocLib/FR</my:BaseDocFolder>
        <my:SectionURL1>
        </my:SectionURL1>
        <my:SectionURL2>
        </my:SectionURL2>
        <my:SectionURL3>
        </my:SectionURL3>
        <my:SectionPic1>
    
    </my:myFields>


    Tried below powershell but it isn't working:

    $URL1 = 'NewManagementServer'
    $xml=New-Object XML
    $path = 'C:\Users\inanns04\Downloads\FR01102746.xml'
    $xml.Load($path)


    $mgr=new-object System.Xml.XmlNamespaceManager($xml.Psbase.NameTable)
    $mgr.AddNamespace("my",$xml.configuration.xmlns)
    $ElementToChange =$xml.SelectSingleNode("//my:URL1",$mgr)
    $ElementToChange.InnerText("newtext")
    $xml.Save($path)

    Got the below error:

    You cannot call a method on a null-valued expression.
    At C:\Users\Sachchin\Desktop\Untitled1.ps1:17 char:27
    + $ElementToChange.InnerText <<<< ("newtext")
        + CategoryInfo          : InvalidOperation: (InnerText:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Wednesday, January 3, 2018 5:20 PM

Answers

  • Hi,

    Seems you don’t past full xml((there is no end tag for <my:FileAttachments>) .

    Here is my test script:

    #$URL1 = 'NewManagementServer'
    #$xml=New-Object XML
    $path = 'C:\Lee\Script\FR01102746.xml'
    #$xml = (Get-Content $path)
    $newValue='newtext'
    
    #$mgr=new-object System.Xml.XmlNamespaceManager($xml.Psbase.NameTable)
    #$mgr.AddNamespace("my",$xml.configuration.xmlns)
    #$ElementToChange =$xml.SelectSingleNode("//my:URL1")
    #$ElementToChange.InnerText("newtext")
    [xml]$xml = Get-Content $path       
    $xml.myFields.FileAttachments.URL1=$newValue
    $node = $newValue
    $xml.Save($path)

    Best Regards,

    Lee


    Thursday, January 4, 2018 3:41 AM

All replies

  • Hope the below helps

    https://stackoverflow.com/questions/14905396/using-powershell-to-read-modify-rewrite-sharepoint-xml-document


    Please remember to click Mark as Answer on the answer if it helps you

    Thursday, January 4, 2018 1:01 AM
  • Hi,

    Seems you don’t past full xml((there is no end tag for <my:FileAttachments>) .

    Here is my test script:

    #$URL1 = 'NewManagementServer'
    #$xml=New-Object XML
    $path = 'C:\Lee\Script\FR01102746.xml'
    #$xml = (Get-Content $path)
    $newValue='newtext'
    
    #$mgr=new-object System.Xml.XmlNamespaceManager($xml.Psbase.NameTable)
    #$mgr.AddNamespace("my",$xml.configuration.xmlns)
    #$ElementToChange =$xml.SelectSingleNode("//my:URL1")
    #$ElementToChange.InnerText("newtext")
    [xml]$xml = Get-Content $path       
    $xml.myFields.FileAttachments.URL1=$newValue
    $node = $newValue
    $xml.Save($path)

    Best Regards,

    Lee


    Thursday, January 4, 2018 3:41 AM
  • Thanks Lee for your response let me try this script and update your about the same.
    Friday, January 5, 2018 2:41 PM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, so it may help others who has similar requirement.

    Best Regards,

    Lee


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

    Thursday, January 11, 2018 1:43 AM
  • Hello Lee,
    I modified the script to check and edit the urls from infopath forms and was able to do so:

    To accomplish this.. i tried below steps:

    • I mapped the infopath library on to my local system so that i can run my script on all the forms.
    • I was able to change the urls within the form using the below script:
    $files = Get-ChildItem Z:\ #-recurse  #| Format-Table FullName
    foreach ($file in $files) 
    {
        $path = $file.FullName
        #Write-Host "Path:"$path
        [xml]$xml = Get-Content $path
       
       
        $url1 = $xml.myFields.FileAttachments.URL1
        $url1Read = $xml.myFields.FileAttachments.URL1Read
        $url2 = $xml.myFields.FileAttachments.URL2
        $url2Read = $xml.myFields.FileAttachments.URL2Read
        $url3 = $xml.myFields.FileAttachments.URL3
        $url3Read = $xml.myFields.FileAttachments.URL3Read
        $baseDocFolder = $xml.myFields.FileAttachments.BaseDocFolder
        $saveForm = "false"
        Write-Host  "Path:"$path
        
        if ($url1 -and $url1 -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL1"$url1
            $url1=$url1.replace("HTTP://oldSPURL/","http://newSPURL/")
            Write-Host "NewURL1"$url1
            $xml.myFields.FileAttachments.URL1=$url1
            $saveForm="true"
        }
            if ($url1Read -and $url1Read -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL1"$url1Read
            $url1Read=$url1Read.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "NewURL1"$url1
            $xml.myFields.FileAttachments.URL1Read=$url1Read
            $saveForm="true"
        }
        
            if ($url2 -and $url2 -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL2"$url2
            $url2=$url2.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "NewURL2"$url2
            $xml.myFields.FileAttachments.URL2=$url2
            $saveForm="true"
        }
           if ($url2Read -and $url2Read -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL1"$url2Read
            $url2Read=$url2Read.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "NewURL1"$url2Read
            $xml.myFields.FileAttachments.URL2Read=$url2Read
            $saveForm="true"
        }
        
        if ($url3 -and $url3 -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL3"$url3
            $url3=$url3.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "NewURL3"$url3
            $xml.myFields.FileAttachments.URL3=$url3
            $saveForm="true"
        }
        
          if ($url3Read -and $url3Read -Match "HTTP://oldSPURL/")
        {
            Write-Host "URL1"$url3Read
            $url3Read=$url3Read.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "NewURL1"$url2Read
            $xml.myFields.FileAttachments.URL3Read=$url3Read
            $saveForm="true"
        }
        
        if ($baseDocFolder -and $baseDocFolder -Match "HTTP://oldSPURL/")
        {
            Write-Host "baseDocFolder"$baseDocFolder
            $baseDocFolder=$baseDocFolder.replace("HTTP://oldSPURL/","HTTP://newSPURL/")
            Write-Host "baseDocFolderURL"$baseDocFolder
            $xml.myFields.FileAttachments.BaseDocFolder=$baseDocFolder
            $saveForm="true"
        }
        if($saveForm -eq "true")
        {
            $xml.Save($path)
            Write-Host "Form has been updated"    
        
        }
        
    }

    But while accesing the forms from the browser, it displays one of the below errors for almost all the 200 forms.

    Saturday, January 13, 2018 7:37 AM