Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • André Lange 108 posts 410 karma points
    May 31, 2019 @ 13:08
    André Lange
    0

    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);
                        }
                    }
                }
    
  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 31, 2019 @ 13:36
    Søren Kottal
    100

    Hi André

    You should be able to do like this

    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

  • André Lange 108 posts 410 karma points
    May 31, 2019 @ 14:38
    André Lange
    0

    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.

    home.Descendants<IConfiguration>(x => x.HideFromSitemap); 
    

    Are you using umbraco 8 as well ?

Please Sign in or register to post replies

Write your reply to:

Draft