16

Why am I getting this error with the code below ParseException: End of string expected at position 4.

Here's the code: The error is on the 3rd line.

var db = Sitecore.Configuration.Factory.GetDatabase("web");
string query = @"fast:/sitecore/content/foodservice/home/Products/3492-5326/3518-7";
Item item = db.SelectSingleItem(query);
return item;

Can we use a fast query with SelectSingleItem()? I was trying to avoid the get folder contents and loop through each item until I find the target solution. Suggestions?

2

2 Answers 2

18

When using Sitecore Query, you need to escape item names with dashes. From SDN:

Items with dashes in names (“-”) should be included in “#” symbols. Please use such syntax: /sitecore/content/Home/#About-us#. Otherwise you may get the following error: Invalid lookup source "/sitecore/content/TestSiteB/Home/About-us": End of string expected at position 38. Also, an item name should be included in “#” symbols if it contains “and” or “or” word and is used before “//” symbols. For example: "/sitecore/content/Home/#news and events#//".

UPDATE: I have confirmed that this applies to Fast Query as well.

End of string expected at position 27. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Sitecore.Data.Query.ParseException: End of string expected at position 27.

Source Error: 


Line 21:         protected void Page_Load(object sender, EventArgs e)
Line 22:         {
Line 23:             Sitecore.Context.Database.SelectSingleItem("fast:/sitecore/Content/Home/Test-Item");
Line 24: 
Line 25:             Model = Sitecore.Context.Item;

The same code runs fine with #escapes#:

Sitecore.Context.Database.SelectSingleItem("fast:/sitecore/Content/Home/#Test-Item#");
7
  • I'm using Sitecore Fast Query, not Sitecore Query. The dashes are not the problem. Commented Nov 17, 2010 at 20:50
  • Are you sure? Have you tried escaping them? My instinct would be that Fast Query would utilize the same parsing logic, making the dashes a requirement. Commented Nov 20, 2010 at 19:40
  • 1
    I'm positive it's not the dashes. My error is at position 4 and there is not a dash any where around it. I used another solution. Thanks! Commented Nov 24, 2010 at 2:25
  • 2
    Could you post your solution here then for the next person who has an issue such as this? Commented Nov 24, 2010 at 9:50
  • 1
    To answer my own question above - one can simply replace all dashes in the path with dashes surrounded by hashes (#) like: item.Paths.Path.Replace("-", "#-#").
    – Paul
    Commented Sep 7, 2018 at 2:13
8

This is a bug when using Fast Query and SelectSingleItem(). The issue is fixed in the Sitecore CMS 6.3.0 rev.100716 release. See Release Notes. Since we are using 6.2, I had to rewrite my method to use a Sitecore Query instead of the Fast Query.

4
  • 1
    Appears that this is now an issue in 6.5.0 (rev. 121009) but for Sitecore query (but at position 5). Joy.
    – longda
    Commented Mar 21, 2013 at 21:38
  • This is also an issue in Sitecore 6.6, so if the bug was fixed, it was re-introduced.
    – Mark
    Commented Nov 18, 2014 at 19:52
  • We are using 8.1 update 2 and also encountering this. If the query is prefixed with "fast:" or "query:", it simply does not work.
    – Markos
    Commented Mar 17, 2017 at 14:15
  • 1
    Still an issue on Sitecore 9.2 Commented May 26, 2020 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.