问题
I have Sitecore folder named MyItems with items of type MyItem. I need to query the items from .net code either with sitecore query or with xpath. MyItem has field MyField of type TreelistEx. I need to select all items where MyField contains 'thevalue' (guid of other item). How can i do it?
Thanks a lot
回答1:
string query = string.Format("/sitecore/content/MyItems/*[contains(@MyField,'{0}')]", thevalue);
Item[] myItems = Sitecore.Context.Database.SelectItems(query);
I just pulled this code from my site and adjusted the names for your query. It is fairly inefficient if you have a lot of MyItems, so I wouldn't use this on a page where performance is key. The same query should work for any list-type field.
回答2:
Although you can't use functions in fast query, you can use sql like syntax with wildcards. Behold:
string query = string.Format("fast:/sitecore/content/MyItems/*[@MyField='%{0}%']", thevalue);
True story. Word.
Source: Page 9 of "Using Sitecore Fast Query" which I believe is behind an "account wall(tm)"
Functions are not supported. However the contains() function can be replaced by a string equality operator that contains SQL wildcards. For example: //[contains(@Title, 'Sitecore')] is equivalent to fast://[@Title = '%Sitecore%']"
回答3:
You may want to use fast query as well
string query = string.Format("fast:/sitecore/content/MyItems/*[contains(@MyField,'{0}')]", thevalue);
update: sitecore
fast query doesn't support functions, so the contains()
function will nor work in this query.
来源:https://stackoverflow.com/questions/6536201/sitecore-select-items-by-field-treelistex