CS Dev Guide: Tags
Tags are a new feature in Community Server 2.1. Generally tags became a frequently used feature in web applications and websites in latest 2-3 years and Community Server team decided to implement them for this great up to date platform.
In previous versions of Community Server, each application type had its own mechanism to categorize its content. Categories for blog posts and subfolders of file gallery folders were two examples of these mechanisms. But in Community Server 2.1 all previous mechanisms are replaced with tags. This replacement wasn't a deep change from base because most old APIs are still valid and no change is made for them. For example, you can use old blog category APIs (I wrote about them here) in order to select, add, remove and edit tags for blog posts.
On the other hand some new APIs have been added to let you add your forum posts under some specific tags and the way that old categories had been used is replaced with a new approach to show tags in tag clouds.
Another important feature is the ability to list all tags per site, per application, per group or per section and new APIs are added to enable these capabilities. For example if you navigate to this page, can see a global tag cloud for all of my tags on my site.
Primary goal of this CS Dev Guide post is to describe new APIs to get tags for a site, a specific application, a group or a section. Mechanisms to add, remove or delete tags for specific applications are different and depend on that application type. Another topic that will be covered is to get a list of posts that match a tag or a list of tags.
CommunityServer.Components.Tag is code representation of a tag. It's very simple and has two properties to specify its name and total count of posts that are tagged under it.
CommunityServer.Components.Tags namespace has a collection of static functions with several overloads which get some parameters and return an ArrayList of Tag objects for a site, an application type, a group or a section. There are some other static methods that remove unused tags.
The purpose of these methods is obvious from their name and their parameters represent where an overload is helpful. Although there are many methods in this namespace but most of them are overloads. Therefore I list all method names and describe their purpose here:
- GetPostsMatchingTags: Returns a CommunityServer.Components.SearchResultSet object of all posts that match tags based on the parameters that are passed to it.
- GetTags: Returns an ArrayList of Tag objects for all available tags on a site or a specific application type.
- GetTagsByGroup: Returns an ArrayList of Tag objects for a specific group.
- GetTagsByGroups: Returns an ArrayList of Tag objects for some specific groups.
- GetTagsBySection: Returns an ArrayList of Tag objects for a specific section.
- GetTagsBySections: Returns an ArrayList of Tag objects for some specific sections.
- RemoveUnusedTags: Removes unused tags for a site or a specific application type.
- RemoveUnusedTagsFromGroup: Removes unused tags for a specific group.
- RemoveUnusedTagsFromGroups: Removes unused tags for some specific groups.
- RemoveUnusedTagsFromSection: Removes unused tags for a specific section.
- RemoveUnusedTagsFromSections: Removes unused tags for some specific sections.
Here there is a general point that should be mentioned: all above methods identify a group by its GroupID and a section by its SectionID. So they get their parameters as integer values for GroupID and SectionID or an array of integers for GroupIDs and SectionIDs.
Now that we discovered these theories, let's take a look at some examples.
In first example, GetPopularPostID() function returns the PostID for most popular post (post with more views in first page of results when page size is 15) in Weblog application that is tagged under .NET, C# or Community Server. In this code GetPostsMatchingTags() static method is used to get an array of tag names, an ApplicationType enumerator and two integer values for page index and page size of results.
int GetPopularPostID()
{
String[] tags = { ".NET", "C#", "Community Server" };
SearchResultSet set = Tags.GetPostsMatchingTags
(ApplicationType.Weblog, tags, 0, 15);
int mostViews = 0;
int postID = 0;
foreach (Post post in set.Posts)
{
if (post.Views > mostViews)
{
mostViews = post.Views;
postID = post.PostID;
}
}
return postID;
}
Second example, GetActiveTagName() function, uses GetTagsByGroup() static method to return a tag name with more posts for current blog group.
String GetActiveTagName()
{
CSContext context = CSContext.Current;
ArrayList tags = Tags.GetTagsByGroup(context.BlogGroupID);
int postCount = 0;
String mostActiveName = String.Empty;
foreach (Tag tag in tags)
{
if (tag.TotalCount > postCount)
{
postCount = tag.TotalCount;
mostActiveName = tag.Name;
}
}
return mostActiveName;
}
Third and last example, RemoveUnusedTagsFromThisSection() method (and a novel after this name!), uses RemoveUnsedTagsFromSection() static method to remove all unused tags for current section.
void RemoveUnusedTagsFromThisSection()
{
CSContext context = CSContext.Current;
Tags.RemoveUnusedTagsFromSection(context.SectionID);
}
Now playing: Ricky Martin - Save The Dance
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
15 Comments : 11.27.06
Feedbacks
Hi,
Im trying to add a Related Posts section to the /Themes/Blogs/[theme]/post.aspx page. I found it interesting that this is not an option in the default installation.
Looking at the <CSBlog:WeblogPostList> control, how can I set the QueryOverrides.Tags property to dynamically populate from the BlogPage context?
Thanks

#1
J-O Eriksson
11.27.2006 @ 12:55 PM