Copied to clipboard

Flag this post as spam?

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


  • cheil 10 posts 90 karma points
    Jan 12, 2016 @ 00:03
    cheil
    0

    AncestorOrSelf/GetSibling for IPublishedContent

    Scenario: I have a Site Components folder as a child of the Home Node which has a header item with all the links, logo, etc. Several levels below the Home node will be microsites which need their own header: I will be creating Site Components folders as a child item for these pages with a header item for each of those microsites. I am using GlassMapper to generate strongly typed content items to pass into my header partial.

    Progress: I am using uQuery's GetAncestorOrSelfNodes() along with GetSiblingNodes() to find the folder's alias type and returning that. Unfortunately uQuery is A) apparently legacy and not best practice and B) does not work with GlassMapper.

    Question: So my question is: what is the best way to perform a query for AncestorOrSelf/GetSibling using IContent or IPublishedContent?

    Also, I looked into Lucene Search but it seems like a generic search result tool, is there a way I could use this to accomplish my goal?

    Thanks

    Edit:

    Writing it out must have cleared my brain a little, because I just got a "working" solution. It seems the IContent method "Ancestors" works in a similar fashion minus the self part in that it will look through all ancestors of the IContent item, not just the first.

    That being said, I am not confident in my work. Could someone spot check this and tell me if there are better ways to approach this?

    var pageId = UmbracoContext.PageId;
    var item = Services.ContentService.GetById(pageId.Value);
    
    //Check current Item for "Site Components" folder. I would prefer to use alias here...
    var siteComp = item.Children().Where(m => m.Name == "Site Components").FirstOrDefault();
    
    //If current item does not have it, then search ancestors
    if (siteComp == null)
    {
        siteComp = item.Ancestors()
            .Select(m => m.Children().FirstOrDefault(n => n.Name == "Site Components"))
            .Where(m => m != null)
            .LastOrDefault();
    }
    
    //Once folder is found, check its children for an item named "Header". I would prefer to use alias here...
    var header = siteComp.Children()
        .Where(m => m.Name == "Header")
        .Select(m => _glassService.CreateType<Header>(m))
        .FirstOrDefault();
    
  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jan 13, 2016 @ 14:03
    Alex Skrypnyk
    2

    Hi Cheil,

    I prefer to rewrite it like that:

    //Check current Item for "Site Components" folder. I would prefer to use alias here...
    var siteComp = Umbraco.AssignedContentItem.Children().FirstOrDefault(m => m.DocumentTypeAlias == "Site Components");
    
    //If current item does not have it, then search ancestors
    if (siteComp == null)
    {
        siteComp = Umbraco.AssignedContentItem
            .Ancestors()
            .Select(m => m.Children().FirstOrDefault(n => n.DocumentTypeAlias == "Site Components"))
            .LastOrDefault(m => m != null);
    }
    
    //Once folder is found, check its children for an item named "Header".I would prefer to use alias here...
    var header = siteComp
        .Children()
        .FirstOrDefault(m => m.DocumentTypeAlias == "Header");
    

    Thanks, Alex

  • cheil 10 posts 90 karma points
    Jan 14, 2016 @ 02:49
    cheil
    0

    Thanks for the reply, I had not even known about Umbraco.AssignedContentItem. I'll definitely be looking in other places on my site where I can make use of that.

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jan 14, 2016 @ 13:25
    Alex Skrypnyk
    0

    Cheil,

    If you are using Razor with Umbraco, read attentively - https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    A lot of fast and helpful methods there.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft