Keyvan Nayyeri

God breathing through me

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

15 Comments

J-O Eriksson
Nov 27, 2006 12:55 PM
#
Great post, thanks Keyvan!

Keyvan Nayyeri
Nov 27, 2006 1:02 PM
#
Thanks J-O :-)

Community Server Daily News
Nov 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

Daily News List Blog
Nov 27, 2006 2:12 PM
#
Keyvan Nayyeri with a Community Server Developer Guide on Tags. Here's a descriptive excerpt of today's

Prog
Nov 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

Community Server Daily News
Dec 01, 2006 1:59 PM
#
from the editor occasional messages that don't fit anywhere else If you were on CommunityServer.org between

Announcements
Dec 01, 2006 2:54 PM
#
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with

Prog
Dec 01, 2006 3:33 PM
#
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with

Prog
Dec 01, 2006 3:33 PM
#
from the editor occasional messages that don't fit anywhere else If you were on CommunityServer.org between

Sam
Dec 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

Keyvan Nayyeri
Dec 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 :-)

Community Server
Jan 19, 2007 6:20 PM
#
This week... J-O Eriksson provides an overview of the Community Server Service Pack upgrade process with

Paul W
Jun 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

Paul Schofield
Sep 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.

Gerard
Nov 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





Ads Powered by Lake Quincy Media Network