Umbraco 5: Get content from HiveManager using separate application
This article will explain how to create an instance of HiveManager in a separate application (console windows application). HiveManager is a single point to access all data in Umbraco 5, when you are running already compiled web application of Umbraco 5 the HiveManager is already initialize but in case if you need to create an instance of HiveManager from another application you have to do some steps.
Creating instance of HiveManager from Console application
At the start you have to create console application (or any type of application you need) and reference all required libraries. The screenshot below shows which libraries are required to create instance of Hive, in my opinion it’s too much libraries.
The code to create Hive is very simple, but you have to prepare many helper classes before you can create it. below is the code to create HiveManager
IHiveManager hive = new HiveManager(new[]
{
new ProviderMappingGroup(
"test",
new WildcardUriMatch("content://"),
singleReader,
singleWriter,
frameworkContext)
}, frameworkContext);
To create all required instances you have to:
- Create instance of FrameworkContext
- Create instance of ProviderMetadata
- Create instance of NhFactoryHelper
- Create reader and writer providers
Loading content of your website in console application with Hive
After you have instance of Hive you have to get ReaderProvider and ask repository about all items or specific item. The code below shows how to iterate over all items and how to get item by Id
private static void Main(string[] args)
{
var wrapper = new HiveManagerWrapper();
IHiveManager hive = wrapper.GetHiveManager();
IEnumerable<TypedEntity> allDocuments =
hive.GetReader<IContentStore>().CreateReadonly()
.Repositories.GetAll<TypedEntity>();
foreach (TypedEntity entity in allDocuments)
{
foreach (TypedAttribute attribute in entity.Attributes)
{
foreach (var val in attribute.Values)
{
Console.WriteLine("{0}: {1}",
attribute.AttributeDefinition.Name,
val.Value);
}
}
}
Console.WriteLine();
Console.WriteLine("----------------------------------------");
Content mypage =
hive.Query<Content, IContentStore>().SingleOrDefault(
x => x.Id == new HiveId("content://p__nhibernate/v__guid/2c10d5d5bab2488a9af9a026000cadfc"));
foreach (TypedAttribute attribute in mypage.Attributes)
{
Console.WriteLine("{0}: {1}",
attribute.AttributeDefinition.Name,
attribute.Values.FirstOrDefault());
}
}
and after you run application you get content of the website
