Problem:
I have a line of code that
uses Sitecore Fast Query to pull all items + children starting with a site
item, like so:
Item[] allItems = db.SelectItems("fast:" +
sitecorePath + "//*");
Unfortunately, I would get a
Sitecore parsing error at runtime:
ParseException: End of string expected at
position...
Turns out Sitecore doesn't
like hyphens ('-') in any sitecore path when using fast query, which I have a
few distributor sites in a folder which contained hyphens.
Solution:
I create a simple method
that resolves a sitecore path to be Sitecore fast query friendly:
string sitecorePath = "";
if (siteItem.Paths.FullPath.Contains("-"))
{
String[] pathParts = siteItem.Paths.FullPath.Split('/');
for (int i = 0; i < pathParts.Length; i++)
{
if (pathParts[i].Contains("-"))
{
sitecorePath += "#" +
pathParts[i] + "#";
}
else
sitecorePath +=
pathParts[i];
if (i < pathParts.Length -
1)
sitecorePath += "/";
}
}
else
sitecorePath =
siteItem.Paths.FullPath;
Basically, the code adds a "#" at the start/end of any
page name with hyphens. For example:
Before: /Sitecore/content/german-sites/site1
After: /Sitecore/content/#german-sites#/site1
I hope this helps somebody.
Further
Reading:
Comments
Post a Comment