Copied to clipboard

Flag this post as spam?

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


  • Nicolai 48 posts 201 karma points
    Jun 14, 2015 @ 19:23
    Nicolai
    0

    Get all nodes from content with a certain tag

    Sorry for double posting but I have a different problem which I dont think should be in the same thread.

    Is it possible to get all nodes with a certain tag?

    The image below show one of my articles with certain tags: enter image description here

    Now, if I want to find all articles in my content which have one of these tags how can I do that?

    Is it something like:

    var articlesSkarpe = Tag.GetNodesByTag("5 Skarpe");
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 14, 2015 @ 20:29
    Jeavon Leopold
    0

    Hi Nicolai,

    I think you are looking for something like this:

    <ul>
        @{
            var taggedContent = UmbracoContext.Application.Services.TagService.GetTaggedContentByTag("5 Skarpe").Select(x => x.EntityId);
            foreach (var contentItem in Umbraco.TypedContent(taggedContent).Where(x => x != null))
            {
                <li>@contentItem.Name</li>
            }
        }
    </ul>
    

    Jeavon

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 14, 2015 @ 20:39
    Dennis Aaen
    0

    Hi Nicolai

    I think that you can do something like this. This will give you all the pages that have the tag page added.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{
        var allNodesWithTags = CurrentPage.AncestorOrSelf().DescendantsOrSelf().Where("tags != \"\"");
    }
    
    @foreach (var node in allNodesWithTags)
    {
        string[] tags = node.tags.ToString().Split(',');
    
        if (tags.Contains("page"))
        {
            <a href="@node.Url">@node.Name</a>
        }
    } 
    

    Hope this helps,

    /Dennis

  • Nicolai 48 posts 201 karma points
    Jun 15, 2015 @ 06:45
    Nicolai
    0

    Unfortunately none of the suggested solutions are working :-(

    @Jeavon - Your solution doesnt render on page. It returns with the error: Error loading Partial View script (file: ~/Views/MacroPartials/testing.cshtml)

    @Dennis - Your solution does render on page but doesnt show anything (I have changed the page tag)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 15, 2015 @ 06:54
    Jeavon Leopold
    0

    Hi Nicolai,

    It does work as I wrote it and tested it so I could add it to the documentation so it must be something else, could you please post your entrie file contents so I can find out what it is?

    Thanks,

    Jeavon

  • Nicolai 48 posts 201 karma points
    Jun 15, 2015 @ 07:50
    Nicolai
    101

    Hey Jeavon,

    Ofcourse :-)

    Code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <ul>
        @{
            var taggedContent = UmbracoContext.Application.Services.TagService.GetTaggedContentByTag("5 Skarpe").Select(x => x.EntityId);
            foreach (var contentItem in Umbraco.TypedContent(taggedContent).Where(x => x != null))
            {
                <li>@contentItem.Name</li>
            }
        }
    </ul>
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 15, 2015 @ 15:40
    Jeavon Leopold
    0

    Hmm, that works absolutely perfectly for me :(

    Can you share the code where the Macro is called?

  • Nicolai 48 posts 201 karma points
    Jun 15, 2015 @ 16:37
    Nicolai
    0

    Could it be caused by umbraco version?

    Im using Umbraco v6

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 15, 2015 @ 16:44
    Jeavon Leopold
    0

    Oooh, yes I don't think that service existed in v6, I'll see what there was...

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 16, 2015 @ 17:50
    Jeavon Leopold
    0

    Ok, it's a little odd but try this one:

     <ul>
        @{
            var taggedNodes = umbraco.cms.businesslogic.Tags.Tag.GetNodesWithTags("5 Skarpe").Select(x => x.Id);
            foreach (var contentItem in Umbraco.TypedContent(taggedNodes).Where(x => x != null))
            {
                <li>@contentItem.Name</li>
            }
        }
    </ul>
    
  • Nicolai 48 posts 201 karma points
    Jun 16, 2015 @ 18:07
    Nicolai
    0

    I already solved it but I did almost exactly the same as you suggest Jeavon :)

    Solution:

    var matchingNodes = Tag.GetNodesWithTags("5 skarpe");
    
    List<dynamic> listArticleSort = new List<dynamic>();        
    
    foreach(var node in matchingNodes) {        
        listArticleSort.Add(node);          
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 16, 2015 @ 18:10
    Jeavon Leopold
    0

    Great, you might find my suggestion to convert to current IPublishedContent object useful....

  • 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