Solve Sitecore Fast Query Parse Exception

If you are developing any Sitecore application and if you are using Fast Query a lot then you would have got the Parse Exception while querying the Item. In this article Let’s look at How to Solve Sitecore Fast Query ParseException?

What is Fast Query ParseException in Sitecore?

ParseException: End of string expected at position 4.

OR

Invalid lookup source "query:fast:.//*[@@templatekey='sample item']": End of string expected at position 4

Whenever you are using Sitecore query and if the Item name contains “-” (hyphen/dashes) you need to escape it with a character “#” (hash). Also, if the item names have the Words “and” / “or” you need to escape the item name in the fast query using “#”.

How to Solve Sitecore Fast Query  Parse Exception by using Escape Character #

While querying the Item in Sitecore if you have dashes (-) or “and” or “or” in the item name escape the item name with a # character.

Check out the below example where item name consists of the hyphen and it is escaped using a hash character.

var db = Sitecore.Configuration.Factory.GetDatabase("web");
string query = @"fast:/sitecore/content//home/Products/#product-1#";
Item item = db.SelectSingleItem(query);
return item;

Escape “and” / “or” in Sitecore Item

var db = Sitecore.Configuration.Factory.GetDatabase("web");
string query = @"fast:/sitecore/content//home/Products/#johnson and johnson#";
Item item = db.SelectSingleItem(query);
return item;
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Javascript

Javascript URLEncode

Let us take a look at what would be the best approach for Encoding URL using Javascript. There are basically 3 Javascript URLEncode methods available. Types of Javascript URLEncode escape()…
View Post