Sitefinity CMS

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

Glossary Item Box

Updating a Tag is a very simple process:

  1. Get a Tag associated with a transaction
  2. Change its properties
  3. Save the Tag with ContentManager

 

There are three methods for updating a tag:

  • SaveTag(ITag tag) - Save a specified tag item
  • SaveTags(Guid contentId, List<string> tags) - Saves the given tags to the content item with specified ID
  • SaveTags(Guid contentId, List<string> tags, string owner) - Saves the given tags to the content item with specified ID, and a given owner

Save a specified tag item:

SaveTag(ITag tag) 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);
   
// change TagName value
   
firstTag.TagName = "Changed Name of Tag 1";
   
// save the tag
   
contentManager.SaveTag(firstTag);
   Response.Write(firstTag.TagName +
"<br />");
}   

 

Saves the given tags to the content item with specified ID:

SaveTags(Guid contentId, List<string> tags) 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 first content item
   
Telerik.Cms.Engine.IContent firstContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[0]).ID);
   
// get all tags of the given user
   
IList listOfTags = contentManager.GetTags("admin");
   
if (listOfTags.Count > 0)
   {
       
// create a list of strings
       
List<string> listOfStringNames = new List<string>();
       
// add each tag name to this list
       
foreach (Telerik.Cms.Engine.ITag tag in listOfTags)
           listOfStringNames.Add(tag.TagName);
       
if (listOfStringNames.Count > 0)
       {
           
// save tag this content with the specified tags from the list
           
contentManager.SaveTags(firstContent.ID, listOfStringNames);
       }
   }
}  

 

Saves the given tags to the content item with specified ID, and a given owner:

SaveTags(Guid contentId, List<string> tags, string owner) 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 second content item
   
Telerik.Cms.Engine.IContent secondContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[1]).ID);
   
// get all tags
   
IList listOfAllTags = contentManager.GetTags();
   
if (listOfAllTags.Count > 0)
   {
       
// create a list of strings
       
List<string> listOfStringNames = new List<string>();
       
// add each tag name to this list
       
foreach (Telerik.Cms.Engine.ITag tag in listOfAllTags)
           listOfStringNames.Add(tag.TagName);
       
if (listOfStringNames.Count > 0)
       {
           
// save tag this content with the specified tags from the list, for the given user
           
contentManager.SaveTags(secondContent.ID, listOfStringNames, "admin");
       }
   }
}   

 

See Also