Sunday, January 20, 2008

Adding comments to a page in MOSS

I'm a big fan of the Web 2.0 concept. I want to build sites where people are communicating, sharing, and creating something bigger together. Today, company sites and intranets are often very static. How many sites have you not seen that are basically just a hierarchy of information pages created by a few web authors? I want to change this!

A small start would be to allow comments on the typical static information pages, basically the same as a blog entry can be commented upon. Many online news papers have adopted this idea, and I often find myself enjoying the comments more than the article itself!

The basic principle for my implementation is to have a web part presenting the page comments on a page. The comments themselves reside in a list. Bunch together the web part, content part definitions, list definitions and the domain-model implementation into a WSS feature/solution and viola - you're done! I also use IoC and AOP for caching using Spring.Net, but that's just for fun really. :-)

Let me flesh this out a bit.

The domain model business class can have a simple interface like this:

public interface IPageCommentBC
{
void CreatePageComment(PageCommentBE comment);
PageCommentListBE GetPageComments(int pageId);
}

The "data access component" responsible for SharePoint communication (i.e. to read/write to the list) uses a similar interface. Note that you probably will want to request higher access rights in your code so that you may restict access to the list itself but still allow users to read/write using your web part.

The web part gets the page ID using the SharePoint context (SPContext). When it has the page ID it can easily call the business component to get a list of all posts, and also save new posts.

Code for creating a page comment in your web part code behind may look something like this:

// Submit comment.
int pageId = SPContext.Current.ItemId;
IPageCommentBC pageCommentBC = BusinessFactory.PageCommentBC;
PageCommentBE comment = new PageCommentBE();
comment.PageId = pageId;
comment.Title = mTitleTextBox.Text;
comment.Body = mBodyTextBox.Text;
pageCommentBC.CreatePageComment(comment);

The content type is easy. Minimally, you will want to inherit from Item and add two fields - PageId and Body. The content type will thus have Title, PageId and Body fields. The list itself will add default columns such as the creator and creation date. You can add additional columns to the content type to get more functionality, such as for anonymous users, modified functionality and so on.

Of course, you will have to be a bit smart when implementing - for example you will need some algoritm to avoid duplicate posts.

That's it really. Let me know if you want me to add some more information or if you have some comments!

0 comments: