Getting strongly typed content from IPublishedcontent
I am trying to create a xml site map.
And i was wondering, is there a better way or more "correct" way of getting the "HideFromSitemap" property on each page ?
The hidefromsitemap is from a composition that is present on all pages.
It just seems wrong that i first have to check for it, and then cast it to IConfiguration.
Is there a better and or smarter way of doing this ?
My Code:
var home = CurrentPage.AncestorOrSelf<Home>(); // find the home node.
var children = home.Descendants(); // get needed content from each page.
var sitemapList = new List<SitemapModel>(); //initiate new list.
foreach (var item in children)
{
if (item is IConfiguration)
{
var config = (IConfiguration)item;
if (config.HideFromSitemap)
{
var sitemapItem = new SitemapModel()
{
Loc = item.UrlAbsolute(),
LastUpdated = item.UpdateDate,
Priority = "0.5",
ChangeFreq = "weekly"
};
sitemapList.Add(sitemapItem);
}
}
}
var children = home.Descendants<IConfiguration>(x => x.HideFromSitemap); // get needed content from each page, cast them to IConfiguration and filter on HideFromSitemap.
var sitemapList = children.Select(x => new SitemapModel(){
Loc = x.UrlAbsolute(),
LastUpdated = x.UpdateDate,
Priority = "0.5",
ChangeFreq = "weekly"
});
If you have any nodes not implementing the Configuration composition, they will simply not show up in your children list
I was able to do it like this, based on your response :
var home = this.nodeHelper.GetFrontpage(); // find the home node.
var children = home.Descendants<IConfiguration>().Where(x => x.HideFromSitemap == false); // get needed content from each page.
var sitemapList = new List<SitemapModel>(); //initiate new list.
foreach (var item in children)
{
var sitemapItem = new SitemapModel()
{
Loc = item.UrlAbsolute(),
LastUpdated = item.UpdateDate,
Priority = "0.5",
ChangeFreq = "weekly"
};
sitemapList.Add(sitemapItem);
}
But for some reason it would not let me do as you did directly.
Getting strongly typed content from IPublishedcontent
I am trying to create a xml site map.
And i was wondering, is there a better way or more "correct" way of getting the "HideFromSitemap" property on each page ? The hidefromsitemap is from a composition that is present on all pages.
It just seems wrong that i first have to check for it, and then cast it to IConfiguration.
Is there a better and or smarter way of doing this ?
My Code:
Hi André
You should be able to do like this
If you have any nodes not implementing the
Configuration
composition, they will simply not show up in yourchildren
listI was able to do it like this, based on your response :
But for some reason it would not let me do as you did directly.
Are you using umbraco 8 as well ?
is working on a reply...