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?

  • 1
  • 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 '18 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. – Dylan Young Mar 19 '18 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 '18 at 19:52
  • @MarkCassidy so just change the title, content and meaning of the question then... got it ;-) – Matthew Dresser Mar 20 '18 at 16:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.