Copied to clipboard

Flag this post as spam?

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


  • pedman 7 posts 87 karma points
    Feb 15, 2017 @ 08:49
    pedman
    0

    Exclude content ID from siteMap

    Hello.

    I'm trying to make a siteMap, sort of in my own way. I know there are other ways to do this, but I'd like to try this way.

    My site is as follows

    Masterpage

    Index

    About

    Name 1

    Name 2

    Contact

    Something else

    Something else

    I would like to exclude Name 1 and 2.

    I have the ID's of the Contents that I want excluded, but I can't loop through them. I've tried this:

    var siteMap = Umbraco.Content(1064);
    
        foreach(var site in siteMap.Children.Where("Visible"))
        {
            var excludesId = "1174,1175,1176,1178,1180,1181,1182,1183,1184,1185,1186,1187"; 
            <a href="@site.Url">@site.Name</a>
                <br>
                foreach(var child in site.Descendants().Where(x => !x.Id.ToString().CsvContains(excludesId)))
                {
                <a href="@child.Url">@child.Name</a><br>
                }
        }
    }
    

    But it spews an error because of the lambda expression.

    Any suggestions?

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 15, 2017 @ 09:11
    Nik
    1

    Hi Pedman,

    There are a few options available to you.

    1) Instead of having excludesId as a string you could try having it as an array/list.

     var excludesId = "1174,1175,1176,1178,1180,1181,1182,1183,1184,1185,1186,1187".Split(',');
    

    Then you could do:

      .Where(x =>  excludesId.All( id=> id != x.Id.ToString())) 
    

    2) Alternatively, you could add a checkbox property to your documents and call it "exclude from sitemap"

    Then you don't have to have a hard coded list, instead you could do:

     .Where(x=> !x.GetPropertyValue<bool>("excludeFromSitemap"))
    

    The latter would be a much more flexible approach :-)

    Nik

  • pedman 7 posts 87 karma points
    Feb 15, 2017 @ 09:33
    pedman
    0

    var siteMap = Umbraco.Content(1064);

    foreach(var site in siteMap.Children.Where("Visible"))
    {
        var excludesId = "1174,1175,1176,1178,1180,1181,1182,1183,1184,1185,1186,1187".Split(',');
        <a href="@site.Url">@site.Name</a>
            <br>
            foreach(var child in site.Descendants().Where(x=> !x.GetPropertyValue<bool>("excludeFromSiteMap")))
            {
            <a href="@child.Url">@child.Name</a><br>
            }
    }
    

    Throws the same error ("Cannot use a lambda expression as an argument to a dynamically dispatched operation"). I have made a checkbox on the ID's I want excluded, checked those that I want excluded, but still gives me this error.

    Thanks a ton for helping me

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 15, 2017 @ 09:41
    Nik
    101

    Ahh, okay.

    So, personally I am not a fan of dynamic content. If I can I try and use typed content as I prefer it.

    I would change the line where you are getting your siteMap to this:

    var siteMap = Umbraco.TypedContent(1064);
    

    This might resolve the issue relating to dynamics.

    Nik

  • pedman 7 posts 87 karma points
    Feb 15, 2017 @ 09:43
    pedman
    0

    That worked.

    Thank you very much for the fast replies and the precise help :)

    Have a good day :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies