Skip to content

Instantly share code, notes, and snippets.

@jraps20
Last active Oct 3, 2018
Embed
What would you like to do?
namespace MyProject.Pipelines
{
public class ItemsLookup : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
// Shared Data Folder
Context.Items.SetLazy(MultisiteConstants.SharedDataFolderItem, () =>
Context.Database.GetItem(new ID(MultisiteConstants.SharedDataFolderId))
);
// Shared Configurations Item
Context.Items.SetLazy(MultisiteConstants.SharedDataConfigurationItem, () =>
GetItemByName(MultisiteConstants.ConfigurationsItemName, Context.Items.Get(MultisiteConstants.SharedDataFolderItem))
);
// Shared Global Item
Context.Items.SetLazy(MultisiteConstants.SharedDataGlobalItem, () =>
GetItemByName(MultisiteConstants.GlobalItemName, Context.Items.Get(MultisiteConstants.SharedDataFolderItem))
);
// Website Root
Context.Items.SetLazy(MultisiteConstants.RootItem, () => Context.Database.Items[Context.Site.RootPath]);
// Home Item
Context.Items.SetLazy(MultisiteConstants.HomeItem, () => Context.Database.Items[Context.Site.StartPath]);
// Local Configurations Item
Context.Items.SetLazy(MultisiteConstants.LocalConfigurationsItem, () =>
GetItemByName(MultisiteConstants.ConfigurationsItemName, Context.Items.Get(MultisiteConstants.RootItem))
);
// Local Copyright Settings Item
Context.Items.SetLazy(MultisiteConstants.LocalCopyrightSettingsItem, () =>
GetItemByTemplateId(Templates.CopyrightSettings.ID, Context.Items.Get(MultisiteConstants.LocalConfigurationsItem))
);
// Local Global Item
Context.Items.SetLazy(MultisiteConstants.LocalGlobalItem, () =>
GetItemByName(MultisiteConstants.GlobalItemName, Context.Items.Get(MultisiteConstants.RootItem))
);
}
public static Item GetItemByName(string name, Item parentItem)
{
if (parentItem == null)
{
Log.Warn(string.Format("Unable to retrieve item by name: {0}, parentItem is null.", name), new object());
return null;
}
return parentItem.GetChildren()
.FirstOrDefault(i => i.Name == name);
}
public static Item GetItemByTemplateId(ID templateId, Item parentItem)
{
if (parentItem == null)
{
Log.Warn(string.Format("Unable to retrieve item by template id: {0}, parentItem is null.", templateId.ToString()), new object());
return null;
}
return parentItem.GetChildren()
.FirstOrDefault(i => i.TemplateID == templateId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment