Copied to clipboard

Flag this post as spam?

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


  • Patrick Robin 34 posts 104 karma points
    Sep 27, 2017 @ 15:29
    Patrick Robin
    0

    Descendants by Tag

    I'm currently developing a new website powered by Umbraco 7.6.1 and I'm having some trouble porting some code that was running under 7.4.3

    Previously I used the following code to get a list of all descendants from the current node that had a specific tag:

    var tag = Request.QueryString["tag"] ?? "";
    var nodes = CurrentPage.Descendants("page").Where("string.IsNullOrEmpty(@0) || tags.Contains(@0)", tag).OrderBy("CreateDate DESC");
    

    However running this code under 7.6.1 yields no results, but if I use:

    var taggedPages = Umbraco.TagQuery.GetContentByTag(tag);
    

    I get the correct results but I cannot see how I can limit the results to descendants of a specific node nor how I can implement paging.

    Any ideas?

  • Kevin Jump 2342 posts 14869 karma points MVP 8x c-trib
    Sep 27, 2017 @ 15:49
    Kevin Jump
    0

    Hi

    under 7.6.x depending on other settings using dynamic types doesn't work .

    the Typed Version of what you want is.

    Model.Content.DescendantsOrSelf("page")
        .Where(x => !x.HasValue("tag") && x.GetPropertyValue<string>("tag").Contains(tag));
    

    However the current recommendation is try to avoid Descendants calls - so there is probably some clever XPath that can do this quicker.

    However if you are talking a few (100's not 1000s of nodes) you might want to use the TagQuery - which should be quicker and you can do the following to just get the tags that are below the page you are on.

    var taggedPages = Umbraco.TagQuery.GetContentByTag(tag)
        .Where(x => x.Path.StartsWith(Model.Content.Path));
    

    and while this isn't ideal. you can do some pageing with Skip() and Take().

    var pagedContent = taggedPages.Skip(pageNo-1).Take(pageSize) 
    

    this should be ok for a few pages (approx 100 nodes say) but it might slow things down if you have 1000s of pages tagged across your site.

  • Patrick Robin 34 posts 104 karma points
    Sep 27, 2017 @ 16:41
    Patrick Robin
    0

    That's brilliant, thanks for your help, I'll have a play around with your suggestions tomorrow.

Please Sign in or register to post replies

Write your reply to:

Draft