Archive Page Types

Last Updated: 2019-04-01

For general information on the different rules & components available when defining content types, please refer to How To Setup Content.

Archive Pages are a special type of pages that besides having their own content also works as a container for posts. Just like Page Types, Archive Pages are imported using the Piranha.AttributeBuilder package.

Defining an Archive Page Type

Archive Pages works in the exact same way as regular pages besides that they inherit from the base class ArchivePage.

using Piranha.AttributeBuilder;
using Piranha.Extend.Fields;
using Piranha.Models;

[PageType(Title = "Blog Archive", UseBlocks = false)]
public class BlogArchive : ArchivePage<BlogArchive>
{
[Region(Title = "Main Content")]
public MarkdownField Body { get; set; }
}

Controlling Allowed Post Types

By default any type of posts can be added to an Archive Page, but you can specify this by adding one or more PageTypeArchiveItemAttribute to your type.

using Piranha.AttributeBuilder;
using Piranha.Extend.Fields;
using Piranha.Models;

[PageType(Title = "Blog Archive", UseBlocks = false)]
[PageTypeArchiveItem(typeof(BlogPost)]
public class BlogArchive : ArchivePage<BlogArchive>
{
[Region(Title = "Main Content")]
public MarkdownField Body { get; set; }
}

Specifying the Archive Type

By default the PostArchive Property contains posts of the type DynamicPost, the downside of this is that dynamic models are not cached, which will result in a higher load on the database. The reason for this being the default behaviour is for backwards compability and will likely change in the future. You can create a generic Archive Page to allow for greater flexibility.

using Piranha.AttributeBuilder;
using Piranha.Extend.Fields;
using Piranha.Models;

[PageType(Title = "Blog Archive", UseBlocks = false)]
public class BlogArchive<T> : ArchivePage<BlogArchive, T> where T : PostBase
{
[Region(Title = "Main Content")]
public MarkdownField Body { get; set; }
}

Below are a few examples on how this can be used.

Single Type

If you have restricted your archive to contain a single Post Type the most convenient approach is to get all posts as the specified type.

var model = await api.Pages.GetByIdAsync<BlogArchive<BlogPost>>(id);
model.Archive = await api.Archives.GetByIdAsync<BlogPost>(...);

Base Class

If you have multiple allowed Post Types, but want to get all posts as their type you can request them using PostBase. Remember that you have to check what type each post is or use separate DisplayTemplates for them.

var model = await api.Pages.GetByIdAsync<BlogArchive<PostBase>>(id);
model.Archive = await api.Archives.GetByIdAsync<PostBase>(...);

foreach (var post in model.Archive)
{
if (post is BlogPost)
{
...
}
else if (post is NewsPost)
{
...
}
}

Info Class

If you don't need any regions or blocks in your listing, your can request the archive using the PostInfo class. These models are also cached.

var model = await api.Pages.GetByIdAsync<BlogArchive<PostInfo>>(id);
model.Archive = await api.Archives.GetByIdAsync<PostInfo>(...);