Reputation: 20636
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?
Upvotes: 7
Views: 6470
Reputation: 11491
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
Upvotes: 7
Reputation: 563
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>
}
Upvotes: 1
Reputation: 20636
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
Upvotes: 14