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/*

or

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#/*

or

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/*

or

sitecore/content/Home/News-Events/*

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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *