Keyvan Nayyeri

God breathing through me

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 CSContextCSContext.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.UserCSContext.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.

8 Comments

Jose Lema
May 20, 2006 1:16 PM
#
Great post Keyvan! The only thing I think that's worth mentioning is that getting CSContext.Current is a bit expensive. The recommended practice is to only access it once, store it locally, and then access any of the properties from the local copy. For example: CSContext csContext = CSContext.Current; string myAppKey = csContext.ApplicationKey; int groupID = csContext.GroupID; int categoryID = csContext.CategoryID; ...

Keyvan Nayyeri
May 20, 2006 1:47 PM
#
Thanks Jose, Usually I do this in my codes but as here I wanted to talk about each property independetly, I used separate instances. It was an important point to be mentioned here. Thanks man :-)

Community Server Daily News
May 22, 2006 12:52 PM
#
weblogs.asp.net upgraded to CS 2.1.   Head on over and check out the .NET developer...

The Desert Skies
May 22, 2006 3:35 PM
#
We got mentioned in the CS Daily News today, very cool.  Thank you very much.  He got my name...

Daniel
May 25, 2006 9:57 AM
#
Good post - thanks any chance you could tell me how to get the UserId (owner) of the current (csContext.ApplicationKey) Blog/Gallery - tah

Keyvan Nayyeri
May 25, 2006 10:24 AM
#
Here is the code: CommunityServer.Blogs.Components.Weblog objBlog= new CommunityServer.Blogs.Components.Weblog(); objBlog = CommunityServer.Blogs.Components.Weblogs.GetWeblog(CSContext.Current.ApplicationKey); string[] Owners = objBlog.OwnerArray; CommunityServer.Components.User objUser = new User(); objUser.Username = Owners[0]; int ID = objUser.UserID; I didn't test it but the snippet should be similar to this.

The Original .NET Geek
Jun 05, 2006 6:43 PM
#
Community Server Rocks!  Since I have installed my CS site, I have been working to modify it. ...

TrackBack
Sep 29, 2006 2:13 AM
#
The CSContext object in Community Server is more popular than Renee Mattioli in my sixth grade class, and she was the tops.

Leave a Comment





Ads Powered by Lake Quincy Media Network