How to patch an attribute value with a Sitecore Include file
Asked Answered
P

2

8

I need to create a Sitecore include patch file to add a string to the existing value attribute of the IgnoreUrlPrefixes setting in the web.config.

I have tried to overwriting the default ignored prefixes entirely with the following include file:

<?xml version="1.0"?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <sitecore>
            <settings>
                <setting name="IgnoreUrlPrefixes">
                    <patch:attribute name="value">/foo/|/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing</patch:attribute>
                </setting>
            </settings>
        </sitecore>
    </configuration>
</settings>

Where /foo/ is the url prefix that I would like to add to the default prefixes. ShowConfig.aspx identifies that the modified configuration has not been applied.

Ideally I would like to be able to simply add /foo/ to whatever exists as the default IgnoreUrlPrefixes values. Does anyone know if this is possible and how to specify it in Sitecore patch syntax?

Piny answered 25/5, 2013 at 20:31 Comment(0)
V
12

Good explanation of all possibilities of Sitecore include config files can be found in this John West blog post.

As you can find in the linked post:

patch:attribute: Define or replace the specified attribute.

It does not allow to "add /foo/ to whatever exists as the default IgnoreUrlPrefixes" attribute.

Vegetarianism answered 25/5, 2013 at 20:37 Comment(2)
Thanks Maras for confirming that so quickly. I had a typo in my original file which was stopping it from loading. I've ended up using the set:value syntax as it's a little more intuitive for anyone reading the file.Piny
That links now dead unfortunatelyModernize
T
3

I recently ran into this same issue and it seems like Mark Ursino posted a blog on this particular issue:

http://firebreaksice.com/sitecore-patchable-ignore-lists/

In his example, he executes a custom pipeline after the default Sitecore one to update the value.

So instead, I’ve created a new pipeline processor that comes after the built-in one (which will support the existing native IgnoreUrlPrefixes setting) and will allow you to add each path via its own XML config node. The advantage here is you can patch and continue to patch on without needing to copy the existing values.

Sample patch file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="Sitecore.PatchableIgnoreList.ProcessPatchedIgnores, Sitecore.PatchableIgnoreList"
                   patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.IgnoreList, Sitecore.Kernel']">
          <Paths hint="list:AddPaths">
            <foo>/foo</foo>
            <bar>/bar</bar>
          </Paths>
        </processor>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

Source code for the pipeline processor, from the blog:

using Sitecore.Collections;
using Sitecore.Pipelines.HttpRequest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Sitecore.PatchableIgnoreList
{
    public class ProcessPatchedIgnores : HttpRequestProcessor
    {
        private List<string> _paths = new List<string>();

        public override void Process(HttpRequestArgs args)
        {
            string filePath = args.Url.FilePath;

            foreach (string path in _paths)
            {
                if (filePath.StartsWith(path, StringComparison.OrdinalIgnoreCase))
                {
                    args.AbortPipeline();
                    return;
                }
            }
        }

        public void AddPaths(string path)
        {
            if (!string.IsNullOrEmpty(path) && !_paths.Contains(path))
            {
                _paths.Add(path);
            }
        }
    }
}
Tomtit answered 6/3, 2017 at 21:2 Comment(3)
Thanks for the update and code snippet. I do have a reservation around the over use of pipeline processors to handle these sort of tasks. To my mind they have become our golden hammer, fixing all the problems whilst maybe not being the right solution in all cases. For example the configuration patch processor is running in the httpRequestBegin pipeline. That is going to get called a lot, and in most cases the pipeline processor is not relevant to the request being made. Interested to hear your thoughts.Piny
If you take a look at the existing Ignore handler, you'll see it also runs in the httpRequestBegin pipeline, so at least in this particular scenario, it's consistent with the existing architecture. I believe the configuration factory only calls the "AddPaths" when it loads the configuration during initialization, so during request processing it is just checking if the current request should be ignored. If you don't run this check at this point, you can't abort the pipeline and the pipeline will execute, even though the entire purpose is to stop the pipeline.Tomtit
My actual issue with this solution is that it can't update the original list, it just creates it's own "extra" list. Sitecore seems to have not opened up any public way to access the list of ignored URLs so you can't actually change the list already used by Sitecore's processor. That would be the best way...Tomtit

© 2022 - 2024 — McMap. All rights reserved.