Get item based on displayname in sitecore -
Get item based on displayname in sitecore -
i need specific item based on it's displayname, how do that?
for illustration want item /sitecore/content/mysite/about on site translated multiple languages , www.site.com/om (in sitecore /sitecore/content/mysite/om)
there couple approaches can take. efficent leverage content search api, challenge display name excluded index default. simple patch file can prepare this:
class="lang-xml prettyprint-override"><configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <contentsearch> <indexconfigurations> <defaultluceneindexconfiguration> <exclude> <__display_name> <patch:delete /> </__display_name> </exclude> </defaultluceneindexconfiguration> </indexconfigurations> </contentsearch> </sitecore> </configuration>
then code like:
class="lang-cs prettyprint-override">public item getitembydisplayname(string displayname) { var searchindex = contentsearchmanager.getindex("sitecore_master_index"); // sub index name using (var context = searchindex.createsearchcontext()) { var searchresultitems = context.getqueryable<searchresultitem>().where(i => i["display name"].equals(displayname)).firstordefault(); homecoming searchresultitems == null ? null : searchresultitems.getitem(); } }
this assuming on sitecore 7. if you're on sitecore 6, alternative limited , not going perform if content tree large. nonetheless, function might like:
class="lang-cs prettyprint-override">public item getitembydisplayname(string displayname) { var query = string.format("fast:/sitecore/content//*[@__display name='{0}']", displayname); var item = sitecore.context.database.selectsingleitem(query); homecoming item; }
please note horribly inefficent since under covers walking content tree.
sitecore
Comments
Post a Comment