CS Dev Guide: CommunityServer.Components.CSContext
In this post I want to talk about the most common namespace in Community Server: CommunityServer.Components.CSContext.
This namespace is a handy namespace for CS developers. CSContext enables you lots of common information about your application and current request. I can point you to Page.Request in ASP.NET to find a same namespace.
And CommunityServer.Components.CSContext.Current is the most common part of CSContext and 95% of times you use this in CSContext. CSContext.Current gives you useful information but I’ll list some important properties here. You can explore them to get better experience.
First import CommunityServer.Components to your code:
using CommunityServer.Components;
CSContext.Current.ApplicationKey is the string value of ApplicationKey of current section that visitor is viewing. Community Server uses unique string value and unique integer values for individual blogs/photo galleries/file galleries. ApplicationKeys are used in CS urls and it’s very common to deal with them. Using this property, you can retrieve current ApplicationKey but note that if you’re in a page which doesn’t have ApplicationKey, this property is equal to null (Nothing in VB) and you should handle this case. This is a short sample for this property:
string MyAppKey = CSContext.Current.ApplicationKey;
CSContext.Current.ApplicationType gives you the enumeration value of your current requested application type. You know CS consists of several applications such as Weblog, forum, Gallery, Spam, etc. It’s worth to be able to check your current application in many cases:
if (CSContext.Current.ApplicationType == ApplicationType.Forum)
// Do something
CSContext.Curernt.BlogGroupID and CSContext.Current.GroupID are obvious from their name. They are useful in some situations when you are working on blogs homepage or some other sections.
int GroupID = CSContext.Current.GroupID;
int BlogGroupID = CSContext.Current.BlogGroupID;
CSContext.Current.CategoryID gives back the value of current category ID. Note that if you are out of a category, this can raise errors.
int CategoryID = CSContext.Current.CategoryID;
CSContext.Current.Group gives back an instance of CommunityServer.Components.Group to help you deal with current Group properties and methods.
Group objGroup = CSContext.Current.Group;
string CurrentGroupDesc = objGroup.Description;
CSContext.Current.Post returns an instance of CommunityServer.Components.Post object which is your current post in request. If you’re not viewing a post, the result equals to null. This is a very useful and common property.
Post objPost = CSContext.Current.Post;
string PostSubject = objPost.Subject;
CSContext.Current.PostID and CSContext.Current.ThreadID are integer values of current Post or Thread ID. You can use previous property to retrieve the PostID but this is a useful shortcut.
int PostID = CSContext.Current.PostID;
int ThreadID = CSContext.Current.ThreadID;
CSContext.Current.QueryString returns a System.Collections.Specialized.NameValueCollection object which contains all name/value pairs from query. CSContext.Current.QueryText returns string value of query.
NameValueCollection QueryValues = CSContext.Current.QueryString;
string QueryText = CSContext.Current.QueryText;
CSContext.Section returns an instance of current CommunityServer.Components.Section and CSContext.SectionID is just a shortcut to find current Section’s ID. This shortcut is very useful.
Section objSection = CSContext.Current.Section;
int SectionID = objSection.SectionID;
int AnotherSectionID = CSContext.Current.SectionID;
if (SectionID != AnotherSectionID)
// Kill me!
CSContext.Current.Statistics contains good statistic information about your website.
int TotalUsers = CSContext.Current.Statistics.TotalUsers;
CSContext.Current.Url gives back the string value of current url.
string CurrentUrl = CSContext.Current.Url;
CSContext.Current.User returns an instance of CommunityServer.Components.User, CSContext.Current.UserID returns the integer value of current user’s ID and CSContext.Current.UserName gives back the string value of current user’s name.
User objUser = CSContext.Current.User;
int UserID = CSContext.Current.UserID;
string Username = CSContext.Current.UserName;
I tried to list some important and common properties of CSContext.Current here. When dealing with most of these properties you should take care about null objects in your code because if you call a property when it doesn’t exists, you will get a null object.
I’ll try to write some sample codes based on these properties to show them in action.
[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.
8 Comments : 05.19.06

#1
Jose Lema
05.20.2006 @ 1:16 PM