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:

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

 avatar
#1
J-O Eriksson
11.27.2006 @ 12:55 PM
Great post, thanks Keyvan!
admin avatar
#2
Keyvan Nayyeri
11.27.2006 @ 1:02 PM
Thanks J-O :-)
 avatar
#3
Community Server Daily News
11.27.2006 @ 1:56 PM
news of the day a grab bag for what's happening in Community Server Tim Laughlin will be migrating an
 avatar
#4
Daily News List Blog
11.27.2006 @ 2:12 PM
Keyvan Nayyeri with a Community Server Developer Guide on Tags. Here's a descriptive excerpt of today's
 avatar
#5
Prog
11.27.2006 @ 2:26 PM
news of the day a grab bag for what's happening in Community Server Tim Laughlin will be migrating an
 avatar
#6
Community Server Daily News
12.01.2006 @ 1:59 PM
from the editor occasional messages that don't fit anywhere else If you were on CommunityServer.org between
 avatar
#7
Announcements
12.01.2006 @ 2:54 PM
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with
 avatar
#8
Prog
12.01.2006 @ 3:33 PM
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with
 avatar
#9
Prog
12.01.2006 @ 3:33 PM
from the editor occasional messages that don't fit anywhere else If you were on CommunityServer.org between
 avatar
#10
Sam
12.01.2006 @ 7:46 PM
I was fiddling with this last week trying to implement my tag-filtering CS module (which involved selecting particular tags based on a criteria) and only after a lot of debugging did I realise this: When you use the GetTagsBySection or similar methods, unless you set the cacheable property to false (which is bad for performance), the returned arraylist is a *reference* to the object in the cache. That is, if you remove or add anything from the arraylist, the next time you use the method (within the cache timeframe), the cached object it returns will contain the changes you made earlier. To workaround this, make sure you use the Copy method in the arraylist first. This is probably a simple beginner problem, but it was hidden enough to evade me for a while :-P
admin avatar
#11
Keyvan Nayyeri
12.01.2006 @ 8:56 PM
Sam, As you said yourself this is a .NET concept. I don't want to cover .NET programming concepts here. I supposed that my readers are familiar with .NET programming and have enough skills. However, thanks for sharing your experience here :-)
 avatar
#12
Community Server
01.19.2007 @ 6:20 PM
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with
 avatar
#13
Paul W
06.07.2007 @ 11:11 AM
Thanks for the write up. I was looking for this information for some time on CommunityServer.org with on luck. One of the posts led me here and I managed to use these methods (and a few others) to build what I needed. -------------------- Paul Waldschmidt my company: http://www.getdt.com My non profit software: http://www.giftwrapplus.com
 avatar
#14
Paul Schofield
09.15.2007 @ 4:18 PM
Thank you for taking the time to get the Community Server API documented. I was wondering if you had any idea how to programatically add tags to a ForumPost? I'm poring over the SDK and I can't find any information on how this is done. Thanks again for your contributions.
 avatar
#15
Gerard
11.10.2008 @ 7:04 PM

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

Leave a Comment