Umbraco - Get All tags used in a node/group
Asked Answered
V

3

7

I have used GetTags() method under umbraco.cms.businesslogic.Tags.Tag to get all tags under a group or node.

var tags = umbraco.cms.businesslogic.Tags.Tag.GetTags("default");

But with umbraco.cms.businesslogic.Tags.Tag being obsolete now, is there any other better alternative?

Also, does the new library offer tag-based querying of nodes?

Verdugo answered 30/3, 2015 at 8:44 Comment(0)
V
14

Okay, So Umbraco 7 has the new TagService library to deal with tags.

To get all Tags used,

var service = UmbracoContext.Application.Services.TagService;
var blogTags = service.GetAllTags("default");

To get specific tag content GetTaggedContentByTag() is the method exposed,

var sports = service.TagService.GetTaggedContentByTag("Gaming");

It returns the TaggedEntity list and the TaggedEntity object with EntityId property.

Source Courtesy : Jimbo Jones

Verdugo answered 30/3, 2015 at 9:9 Comment(2)
Its also useful to take a look at the new Tag Service: github.com/umbraco/Umbraco-CMS/blob/7.1.5/src/Umbraco.Core/…Nonary
You should use Umbraco Helper for frontend, services don't use the cache and query the DB directly..Edulcorate
E
7

There is no need to call tag service.

In umbraco 7 you can use this:

var tags = Umbraco.TagQuery.GetAllTags();

or

var tags = Umbraco.TagQuery.GetAllTags(group);

And you can use

var mycontents = Umbraco.TagQuery.GetContentByTag("mytag")

To fetch your data

Edlun answered 20/10, 2016 at 12:27 Comment(0)
N
1

I've found limitations with the TagService and have used the following to get a list of tags from a specific set of nodes. Just querying the tags by the group has not worked for me.

var pages = parentpage.Children;   
var allNodesWithTags = pages.Where("tags != \"\"");

List<string> taglist = new List<string>();      
foreach (var node in allNodesWithTags)
{
    taglist.AddRange(node.tags.ToString().Split(','));
}

taglist = taglist.OrderBy(q => q).ToList();

It is then simple to output a list of tags from the child nodes:

@foreach (string tag in taglist.Distinct())
{
    <li><a href="#" class="">@tag</a></li>
} 
Nonary answered 7/5, 2015 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.