Last active
August 29, 2015 14:28
-
-
Save vince-geekcore/69cf0b98146db9a6b9e1 to your computer and use it in GitHub Desktop.
Use Sitecore Content Search (Lucene) Index to retrieve all items from index that include a specific rendering. Don't forget to allow/include "__Renderings" in your index. Cleaner solution could be achieved using ComputedField to index renderings by ID type (for example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Use Sitecore Content Search (Lucene) Index to retrieve all items from index that include a specific rendering | |
/// </summary> | |
/// <param name="templateID"></param> | |
/// <param name="database"></param> | |
/// <param name="indexName"></param> | |
/// <returns></returns> | |
private List<Item> GetAllItemsBasedOnRendering(ID renderingID, string database, string indexName) | |
{ | |
List<Item> resultList = new List<Item>(); | |
var index = ContentSearchManager.GetIndex(indexName); | |
Database db = Sitecore.Configuration.Factory.GetDatabase(database); | |
if (db == null) | |
{ | |
return resultList; | |
} | |
using (var context = index.CreateSearchContext()) | |
{ | |
string renderingStr = renderingID.ToString().ToLowerInvariant(); | |
var results = context.GetQueryable<SearchResult>().Where(x => x.Renderings.Contains(renderingStr)).GetResults().Hits; | |
if (results != null && results.Count() > 0) | |
{ | |
resultList = results.Select(x => x.Document.GetItem()).ToList(); | |
} | |
} | |
return resultList; | |
} | |
public class SearchResult : SearchResultItem | |
{ | |
[IndexField("__Renderings")] | |
public string Renderings { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment