views:

33

answers:

1

Sitecore provides a way of escaping words within a Sitecore query that contain characters that they don't like. Such characters include hyphens and spaces. In the interest of simplifying my life, I wrote a simple helper function that would escape every part of a Sitecore query, and it worked fine for a while:

public static string EscapePath(string path){
    return Regex.Replace(path, @"([^/]+)", "#$1#").Replace("#*#", "*");
}

(the Replace("#*#","*") is in there because Sitecore doesn't like it when you wrap the asterisk in hashes).

As I said, this worked fine for a while. Today, I came across a situation where this fails:

EscapePath("/sitecore/content/Seattle/OR/00010046");

The escaped sequence looks innocent enough:

/#sitecore#/#content#/#Seattle#/#OR#/#00010046#

but the query failed within Sitecore with the message Identifier, GUID or "*" expected at position 44. I narrowed the problem down to the #OR# in the query, and suddenly realized what was going on. Apparently Sitecore takes the lone word OR, even when escaped, to mean that you're joining two or more queries together (that is, to be the reserved-word OR). The obvious fix is to replace all instances of #OR# with *[@@name='OR'], and that works just fine. However, that, to me, looks like a hack.

I know that this will most likely only happen with nodes named OR and AND, but I can't find any documentation on the SDN that talks about any reserved words within Sitecore Query, and there's no mention on how to properly escape a query, beyond wrapping the query in hashes.

Is there currently a standard way of escaping queries where I'd be guaranteed to not run into this problem? Or, even better, a document out there outlining all the reserved words in Sitecore Query? I could probably stick to the XPath syntax, and just deal with the (documented) edge cases with escaping those values, but I'd like to stick to Sitecore Query if possible.

+1  A: 

You can see the list of 'reserved' words that will throw this exception on the following method

Sitecore.Data.Query.QueryTokenBuilder.Identifier(string)

Essentially the list is:

  • ancestor
  • and
  • child
  • descendant
  • div
  • false
  • following
  • mod
  • or
  • parent
  • preceding
  • self
  • true
  • xor

In my limited research I didn't see a way to escape these keywords so you may want to hard code around this list.

Sean Kearney