Sitecore query with hypen or dashes


Sometimes your sitecore query might contains hypen or space and you get an error: “unterminated literal string in Query”. It is because the query contains an illegal character and the solution is to escape the “-“.So we need to add hash(#) around the word in the query.

Query: /sitecore/content/My Site/Event/ABC-Items

Earlier I referred the below article and implimented in one of the project:

Escaping dashes/“-” in Sitecore Queries. Datasource query Update

This is actually the nice example. But while working on another project, I applied below piece of code which I think is better and easy solution then the one given in above link.


StringBuilder startItemPath = new StringBuilder(@"/");
startItemPath.Append(string.Join("/", myItemsFolder.Paths.FullPath.Split(new char[] { '/' },
StringSplitOptions.RemoveEmptyEntries).Select(x => string.Format("#{0}#", x))));

allItems = contextDb.SelectItems("fast:" + startItemPath + "//*[@@templateid = '" + templateID + "']").ToList();

By applying above solution. The query will contain “#” around all the words in query. It won’t create any harm for the query to execute.

If you know any other better approach then do comment below.

Happy Querying!


Leave a Reply

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