End of string expected at position

End of string expected at position” if you are facing this error definitely you are working on sitecore query. There are lot of articles and blogs available on sitecore query. I usually refer to these master pieces “CheatSheet” and “Sitecore Query Syntax” when I get stuck with sitecore query, it’s like a shorthand revision note that we use during our exams. CheatSheet also provides information about escaping xpath keywords and much more.

Now let’s move back to the problem statement, my sitecore query was working fine for few items and was giving above error for few other items. I compared the items and concluded that it had to do with the xpath keywods like “and” and “or” in the item names. To be cent percent sure, I tried the query with xpath builder tool in developer center and had the same error displayed.

After googling found on stackoverflow and other blogs,found that this error arises in some other situations also, hence thought why not to bring all the solutions under one roof so here I will mention all the different issues and solutions. Am I missing something? comment on this post and I will include that too.

Symbol # comes to our rescue and is used as an escape sequence character. Hence the solution to most of the problems related to sitecore query can be resolved using it.

1) Query contains reserved keywords

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

So if your query is like

sitecore/content/Home/News and Events/*


sitecore/content/Home/News/following up with media/*

it will yield you end of string expected error.

Solution: Reframe your query using # as below

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


sitecore/content/Home/News/#following up with media#/*

A C# approach to sitecore query might be,

var childItem = Sitecore.Context.Database.Select(string.Format(“{0}//*”),Sitecore.Context.Item.Paths.FullPath);

The above code can throw error if fullpath contains keywords. The solution I used was to build a function to escape these keywords something like,

public string EscapeXPathKeywords(string query)
            //Escape xpath keywords from sitecore query  
            return query.ToLower().Replace(" and ", " #and# ").Replace(" or ", " #or# ")
                .Replace(" true ", "#true# ").Replace(" false ", " #false# ")
                .Replace(" div ", " #div# ").Replace(" mod ", " #mod# ").Replace(" - ", " #-# ")
                .Replace("-", " #-# ").Replace("$", " #$# ");
var childItem = Sitecore.Context.Database.Select(string.Format(“{0}//*”), EscapeXPathKeywords (Sitecore.Context.Item.Paths.FullPath));

Did you observed spaces before and after the keywords in string.Replace()? No? well those spaces needs to be there else the replace function will replace your string as

Sitec#or#e/content/Home/News #or# Events/

2) Item name starts with digit or contains dash(“-“)

If your query is something like

sitecore/content/Home/News and Events/2014/*



The solution is similar and use # or write a function similar to EscapeXPathKeyword.

Thinking to write few more blogs on sitecore query and fast query till then keep reading and sharing sitecore knowledge.


Disabling rules cache

There are some hidden settings which we do not find in web.config for sitecore. I was introduced to one such setting by Sitecore Support hence thought why not to share it. I was dealing with a problem related to long start-up time taken up by sitecore in our development environment after we do a build. While analysing the sitecore log entries I found it consistently that after,

Cache created: ‘rules’ (max size: 10 bytes, running total: 470MB)

this log entry, sitecore was taking more than two minutes to write another log entry, so thought this might be the problem for slow start up time. This was not the case though and I was wrong.

I enquired with Sitecore support about how to disable rules cache during sitecore start-up, sitecore support informed that rules cache might not be an issue still when I insisted, sitecore support shared a setting to disable rules cache as below,

<setting name=”Rules.MaxCachedRules” value=”0″ />

Cache will be built in bytes based on numeric specified for value attribute.

After making this entry the rules cache was not getting build during start up and I can see it disabled as can be seen from the log entry.

Cache created: ‘rules’ (max size: 0 bytes, running total: 499MB)

Sitecore support was correct and it didn’t made any difference after I disabled rules cache during start up.

So in case if you wish to disable rules cache you can make use of this setting.

I also found very comprehensive and nice read blogs by Robbert Hock
Sitecore hidden ‘string’ configuration settings
Sitecore hidden ‘caching’ configuration settings