Sitecore query with hypen or dashes

Challenge:

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.

Read More