TransWikia.com

How can I update a Sitecore XML patch file using SIF?

Sitecore Asked by MartinMace on February 20, 2021

I want to leverage SIF with the Set-Xml task to update the value in the datafolder.config patch file. But so far it seems I am not succeeding in finding the correct XPath due to the XML NS in the file.

I am using the following XPath expression:

//configuration/sitecore/sc.variable[@name='dataFolder']/patch:attribute[@name='value']

For your reference, this is the datafolder.config file shipped with Sitecore 8.2 U7:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sc.variable name="dataFolder">
      <patch:attribute name="value">/data</patch:attribute>
    </sc.variable>
  </sitecore>
</configuration>

Is it possible to set the value using SIF and an XPath expression? And if so, which XPath might do the trick? Any help is appreciated.

2 Answers

Okay, I got it working. The problem was with the inability of the SetXml SIF task to provide an XML NS.

So I created a custom task based on the SetXml task with an extra parameter to provide the namespace, the SIF configuration (in my case) then becomes as follows:

"SetDataFolder": {
      "Type": "SetXml",
      "Params": {
        "FilePath": "[joinpath(variable('Site.WebsiteFolder'), 'App_Config', 'Include', 'DataFolder.config')]",
        "XPath": "//configuration/sitecore/sc.variable[@name='dataFolder']/patch:attribute[@name='value']",
        "Namespace": [
          {
            "patch": "http://www.sitecore.net/xmlconfig/"
          }
        ],
        "Value": "[parameter('DataFolder')]"        
      }
    }

To accomplish this, just copy the complete SetXml task from the SIF library, make the necessary modifications to support the namespace in the Select-XML method, and include it in your module loading and be sure to register it using an overwrite of the out-of-the-box module using

Register-SitecoreInstallExtension -Command Invoke-SetXmlTaskEx -As SetXml -Type Task -Force

Hopefully this will help anyone else stumbling into the same problem until it hopefully becomes part of the official SIF version.

Correct answer by MartinMace on February 20, 2021

As Martine Said you either need to create Extension for SetXml to support namespaces, or in simple cases like DataFolder you can use some trick like this:

"CopyDataFolderConfig": {
        "Type": "Copy",
        "Params": {
            "Source": "[joinpath(variable('Site.PhysicalPath'), 'App_Config','Include','Examples','DataFolder.config.example')]",
            "Destination": "[joinpath(variable('Site.PhysicalPath'), 'App_Config','Include', 'DataFolder.config')]"
        }
    },
    "PatchDataFolder": {
        "Description": "Changing Data Folder.",
        "Type": "SetXml",
        "Params": {
            "FilePath": "[joinpath(variable('Site.PhysicalPath'), 'App_Config','Include', 'DataFolder.config')]",
            "XPath": "//configuration/sitecore/sc.variable[@name='dataFolder']/*",
            "Value": "[variable('Site.DataFolder')]"
        }
    }

using '*' as a selector in such cases is not generaly a good idea, but in this case it wont cause any problem.

Answered by Ghodrat Ashournia on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP