Umbraco - Get All tags used in a node/group

6.3k views Asked by At

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?

3

There are 3 answers

2
Shaunak D On BEST ANSWER

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

0
Ashkan S On

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

0
Jimbo Jones On

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>
}