main banner

Development

Sitecore Query error: End of string expected at position

A couple of weeks ago a coworker experienced a serious issue while working with Sitecore Query. He spent a lot of time trying to run a query for an easy search but it was not working. My friend was pretty sure the syntax was well formed but when the query was executed the result was an error :

End of string expected at position ##



He tried the Sitecore Query in the XPath Builder tool from the Sitecore’s Developer Center and verified the same error was displayed there. I was walking by his cubicle and we took a look at the query at XPath Builder, we noticed that the position mentioned by the error was an separator character in the field’s name, it was composed by two words. We were facing a case of  Sitecore Query is waiting for the End Of String  that never arrives. It was for sure for us the field name must be enclosed by some symbol... we use our developer logic and sixth sense to remember all symbols used for this purposes but none of them worked at all.


He continued researching and found a post that explained the ‘#’ symbol is used to enclose special item’s names every time a special character were included. He added the ‘#’ and the sitecore query worked like a charm.


A name created from more than one word is just one example of item names that must be enclosed by the ‘#’ symbol. Besides the dash character, that replaces the empty space between words in a name, there are other scenarios that require attention as described in the following table based on Sitecore documentation.


Must be enclosed when Item name...

example

contains dashes in name (“-”)

/sitecore/content/#Contact-Us#]

contains “and” or “or” word and is used before ‘//*’ symbols

/sitecore/content/Home/#News and Events#]//*

Starts with digits

/sitecore/content/Home/News/#2013#/January


Inspired in this dark issue, I tried some Sitecore Queries using XPath Builder with the results displayed in the following table.


/sitecore/content/home//*[@Latest

News Header='Latest News']

WORKED: Found 5 results

/sitecore/content/home//*[@#Latest News Header#='Latest News']

WORKED: Found 5 results

/sitecore/content/home//*[@News and events='Find the event']

FAILED: Type Mismatch

/sitecore/content/home//*[@#News and events#='Find the event']

WORKED: Found 1 results

/sitecore/content/home//*[@Join-Us='Contact email']

FAILED: Type Mismatch

/sitecore/content/home//*[@#Join-Us#='Contact email']

WORKED: Found 1 results

/sitecore/content/home//*[@1st='First Story']

FAILED: Identifier expected at position 28.

/sitecore/content/home//*[@#1st#='First Story']

WORKED: Found 1 results



It’s important to remark that although the ‘#’ symbol fixed the issue, there is another post that mentions the “End of string expected” is a symptom of a bug that appears when you’re using Fast Query and SelectSingleItem with Sitecore 6.2. The same post mentions it will be fixed in 6.3... Meanwhile the workaround is to use Sitecore Query instead of Fast Query.


You can get more details from the following Sitecore reference documentation here.


Luis G.

An expert we all come to when in doubt, Luis was born in Monterrey and has a B.S. Electronic Systems Engineering degree His multiple abilities are backed 10 years of experience in .NET, SQL and recently joined the ranks of front end developers. A Star Wars and Lego enthusiast, he is also a loving father and a very friendly co-worker who writes for the Inflection Point’s blog with lots of inspiration and fills our blog with descriptions of our work environment that recall us all of the good times.

Articles