I am needing a full sitemap including post. Currently I am I am doing the below but it feels heavy handed.
/// <summary>
/// Gets the sitemap with the specified id or default sitemap if Empty.
/// </summary>
/// <param name="id">The unique id</param>
private Sitemap GetSitemap(Guid? id = null)
{
var model = id == null ? api.Sites.GetSitemap() : api.Sites.GetSitemap(id);
foreach (var partial in model)
{
partial.Items = GetSitemap(partial.Id);
if (partial.PageTypeName == "Blog Archive")
{
foreach (var item in GetArchiveItems(partial.Id, partial.Level))
{
partial.Items.Add(item);
}
}
}
return model;
}
/// <summary>
/// Gets the sitemap with the specified id or default sitemap if Empty.
/// </summary>
/// <param name="id">The unique id</param>
/// <param name="level">The archive level</param>
private List<SitemapItem> GetArchiveItems(Guid id, int level)
{
var model = new List<SitemapItem>();
Models.BlogArchive archive = GetArchive(id);
for (int i = 0; i < archive.Archive.Posts.Count; i++)
{
DynamicPost item = archive.Archive.Posts[i];
var smItem = new SitemapItem
{
ParentId = id,
SortOrder = i,
Title = item.Title,
NavigationTitle = item.Title,
PageTypeName = item.TypeId,
Permalink = item.Permalink,
Published = item.Published,
Created = item.Created,
LastModified = item.LastModified,
Id = item.Id,
Level = level + 1
};
model.Add(smItem);
}
return model;
}
/// <summary>
/// Gets the archive for the category with the specified id.
/// </summary>
/// <param name="id">The category id</param>
/// <param name="year">The optional year</param>
/// <param name="month">The optional month</param>
/// <param name="page">The optional page</param>
/// <param name="category">The optional category id</param>
private Models.BlogArchive GetArchive(Guid id, int? year = null, int? month = null, int? page = null, Guid? category = null, Guid? tag = null)
{
Models.BlogArchive model;
if (category.HasValue)
model = api.Archives.GetByCategoryId<Models.BlogArchive>(id, category.Value, page, year, month);
else if (tag.HasValue)
model = api.Archives.GetByTagId<Models.BlogArchive>(id, tag.Value, page, year, month);
else model = api.Archives.GetById<Models.BlogArchive>(id, page, year, month);
return model;
}
Hi there. There looks to be some unnecessary calls in there. I think it could be simplified to this at least:
public Sitemap GetSitemap(Guid? id = null)
{
var model = api.Sites.GetSitemap(id);
foreach (var partial in model)
{
if (partial.PageTypeName == "Blog Archive")
{
partial.Items.AddRange(GetArchiveItems(partial.Id, partial.Level));
}
}
return model;
}
private List<SitemapItem> GetArchiveItems(Guid id, int level)
{
var model = new List<SitemapItem>();
var posts = api.Posts.GetAll(id).Where(p => p.Published >= DateTime.Now).ToList();
for (int i = 0; i < posts.Count; i++)
{
DynamicPost item = posts[i];
var smItem = new SitemapItem
{
ParentId = id,
SortOrder = i,
Title = item.Title,
NavigationTitle = item.Title,
PageTypeName = item.TypeId,
Permalink = item.Permalink,
Published = item.Published,
Created = item.Created,
LastModified = item.LastModified,
Id = item.Id,
Level = level + 1
};
model.Add(smItem);
}
return model;
}
I haven't had time to test it but I hope you get the idea!
Best regards
Thanks!
2 small corrections so if someone else is reading this.
IList does not have an AddRange so you have to cast it.
((List<SitemapItem>)partial.Items).AddRange(GetArchiveItems(partial.Id, partial.Level));
I think it was just a typo but your get post is looking for any published in the future.
var posts = api.Posts.GetAll(id).Where(p => p.Published <= DateTime.Now).ToList();
Yeah sorry, I was on my way out so I just wrote it down quickly 馃榿