Dieses Blog durchsuchen

Donnerstag, 14. August 2014

Sitecore Fast query: How to escape a path with regex (Regular expressions)

As you maybe know:
If you a query that has space in it we need to escape them with #!

This will fail:
fast:/sitecore/templates/User Defined//*[@@templatename='Template field']

Better:
fast:/sitecore/templates/#User Defined#//*[@@templatename='Template']

If you do this in Code Behind here is a regex that helps you to make it propper:        

string sQuery = Regex.Replace(page.Paths.FullPath, "[^/]*[- ][^/]*", "#$0#");


Or find here a litte method that does this:
       /// <summary>
        /// Escapes a Sitecore Query
        /// </summary>
        /// <param name="sQuery"></param>
        /// <returns></returns>
        public static string EscapeSitecoreQuery(String sQuery)
        {
            if (string.IsNullOrEmpty(sQuery))
            {
                return sQuery;
            }
            string sEscapedQuery = Regex.Replace(sQuery, "[^/]*[- ][^/]*", "#$0#");
            return sEscapedQuery;
        }


Last not not Least:
Sitecore Queries Cheat Sheet

Happy regexing.