Link Database
Prev Next |
Sometimes it is required to get all Items in the database based on a given template. Iterating through the entire database may be a very expensive operation though.
However in this particular case, we can resort to the Link database. Link database is used by Sitecore to resolve all the linking issues - what referrers and what references the Item has. And if an item is based on a template, it also counts as a reference from the item to the template. The solution then is very simple: get all the referrers for a given template item.
Below you can see how Link Database is used in the RSS module architecture:
private static Item[] GetFeedsInDatabase(Database database, Language language)
{
TemplateItem feedTemplate = database.Templates[Constants.FeedTemplateID];
if (feedTemplate == null)
{
return null;
}
// Get all items refering to the feed template
ItemLink[] links = Globals.LinkDatabase.GetReferers(feedTemplate.InnerItem);
if (links == null)
{
return null;
}
ArrayList result = new ArrayList(links.Length);
// and filter the referers - we dont need to include masters
foreach(ItemLink link in links)
{
if (link.SourceDatabaseName == database.Name)
{
Item item = database.Items[link.SourceItemID, language];
if ((item != null) && (IsMaster(item) == false))
{
result.Add(item);
}
}
}
return (Item[])result.ToArray(typeof(Item));
}
See also:
Prev Next