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?

Shaunak D
  • 19,751
  • 9
  • 41
  • 72

3 Answers3

13

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

Shaunak D
  • 19,751
  • 9
  • 41
  • 72
  • 1
    Its also useful to take a look at the new Tag Service: https://github.com/umbraco/Umbraco-CMS/blob/7.1.5/src/Umbraco.Core/Services/TagService.cs – Jimbo Jones May 07 '15 at 11:01
  • 1
    You should use Umbraco Helper for frontend, services don't use the cache and query the DB directly.. – Mario Lopez Oct 22 '16 at 09:26
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

Ashkan Sirous
  • 7,211
  • 5
  • 41
  • 64
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>
} 
Jimbo Jones
  • 552
  • 6
  • 28