Skip to content

Instantly share code, notes, and snippets.

@jraps20
Last active October 3, 2018 14:54
Show Gist options
  • Save jraps20/1f04a70ebd5f8fa1415d83d2bc41cf66 to your computer and use it in GitHub Desktop.
Save jraps20/1f04a70ebd5f8fa1415d83d2bc41cf66 to your computer and use it in GitHub Desktop.
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