0

Is there some API utility method which can automatically escape an item path for use in a Sitecore query?

I am building a query string in code which uses an item path. If the item path contains a hyphen or underscore, then that name needs to be escaped in the query (surrounded by #) e.g.

/sitecore/content/my site/home/settings-folder

would need to be escaped to

/sitecore/content/my site/home/#settings folder#

I have code such as:

var query = $"{rootItem.Paths.FullPath}//*[@@templateid='{Templates.MyItemType.TemplateId}']";
var items = db.SelectItems(query);

This fails when the path has a hyphen - or underscore _. I've seen this question related to SPE so I could use that regex, but I wonder if there is already some utility method in the Sitecore API to do this?

5
  • 1
    stackoverflow.com/a/36991386/157833
    – Marek Musielak
    Mar 19, 2018 at 13:07
  • 1
    Fortunately there are better and more efficient ways to get all descendant items of a certain template id that does not involve SlowQuery ;-)
    – Mark Cassidy
    Mar 19, 2018 at 13:20
  • 1
    I agree with @MarkCassidy, if you have code that's recursively getting all items of a certain template, you have much bigger issues than the hyphens. That will never perform as well as the Content Search Api. Mar 19, 2018 at 19:29
  • Open up the question to a broader/alternative solution answer and I'll be happy to add some thoughts :)
    – Mark Cassidy
    Mar 19, 2018 at 19:52
  • @MarkCassidy so just change the title, content and meaning of the question then... got it ;-) Mar 20, 2018 at 16:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.