Sitefinity CMS

Finding Tags Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Generic Content > Tags > Finding Tags

Glossary Item Box

There are several ways to retrieve one or more AnswerItems. Choose an appropriate function given the particular scenario:

Get a specific Tag by its ID:

GetTag(Guid tagId) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all tags
IList listOfAllTags = contentManager.GetTags();
if (listOfAllTags.Count > 0)
{
   
// get first tag. This is redundant - just for demonstration
   
Telerik.Cms.Engine.ITag firstTag = contentManager.GetTag(((Telerik.Cms.Engine.ITag)listOfAllTags[0]).ID);
   
// get the tag by specifying its ID
   
contentManager.GetTag(firstTag.ID);
   Response.Write(firstTag.TagName +
"<br />");
}

 

Get a specific Tag by its Name:

GetTag(string tagName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get tag with specified tag name
Telerik.Cms.Engine.ITag secondTag = contentManager.GetTag("Tag 2");
Response.Write(secondTag.TagName +
"<br />");      

 

Get all Tags:

GetTags() Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all tags
IList listOfAllTags = contentManager.GetTags();
if (listOfAllTags.Count > 0)
{
   
foreach(Telerik.Cms.Engine.ITag tagItem in listOfAllTags)
       Response.Write(tagItem.TagName +
"<br />");
}

 

Get all Tags for a specific content item by passing its ID:

GetTags(Guid contentId) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all content items
IList listOfContentItems = contentManager.GetContent();
if (listOfContentItems.Count > 0)
{
   
// get the third content item
   
Telerik.Cms.Engine.IContent thirdContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[2]).ID);
   
// get all tags for the given content item
   
IList listOfTags = contentManager.GetTags(thirdContent.ID);
   
if (listOfTags.Count > 0)
   {
       
foreach (Telerik.Cms.Engine.ITag tagItem in listOfTags)
           Response.Write(tagItem.TagName +
"<br />");
   }
}

 

Get all Tags for the specified owner:

The term owner is applied for any user who has used a specific tag, and not only who has created the tag. This means that if a tag is only created but no content is tagged with it, the tag will have no owner.

GetTags(string owner) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all tags of specified user
IList listOfTags = contentManager.GetTags("admin");
if (listOfTags.Count > 0)
{
   
foreach (Telerik.Cms.Engine.ITag tagItem in listOfTags)
       Response.Write(tagItem.TagName +
"<br />");
}

 

Get all Tags for the specified owner, sorted:

GetTags(string owner, string sortExpression) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all tags of specified user, sorted by name
IList listOfTags = contentManager.GetTags("admin", "TagName ASC");
if (listOfTags.Count > 0)
{
   
foreach (Telerik.Cms.Engine.ITag tagItem in listOfTags)
       Response.Write(tagItem.TagName +
"<br />");
}

 

See Also