Tuesday, January 3, 2012

The SharePoint 2010 Blog Rating Conundrum

Let’s face it, blogs in SharePoint have always felt tacked on.  In 2010 blogs are stood up by creating a site using the Blog site template.  Easy enough, right?  Yes and no…  Without fail you’ll have a client that wants ratings enabled for their SharePoint 2010 blogs.  Hey, awesome, SharePoint 2010 comes with a robust set of social controls right out-of-the-box, including ratings on just about every list item you can imagine.   This includes blogs, right?

Phew!

But, wait, ratings don’t automatically show up for blog posts right out of the box.  The initial point of failure is the blog “Posts” list which is NOT initially set to allow items in the list to be rated.  Flip that option on and you’re only half way there.   The site definition deployed “Post.aspx” file, which is the end-user oriented single blog post view page, does NOT come with a ratings control.   Ugh…

 

If you have only one blog up on your SharePoint farm, edit the “Post.aspx” file with SharePoint Designer and add a List View Web Part:

  1. Within SharePoint Designer, open "Posts.aspx" in advanced mode.
  2. Select an empty area and select "Insert" then click "List View".
  3. Select “Posts” and a new List View of the Posts list will be placed on the page.
  4. Customize this List View to display only rating information.
  5. Define a parameter to assign to the query string variable "ID", with a default value as "-1".

This is a very common SharePoint 2010 workaround.

 

Now if you have several blogs within your farm, you might start looking for options.  Cracking open the Blog site definition might appear attractive at first glance.   There’s a lot of work that goes into creating a one-off blog site definition, and then you may confuse end users or even power users with new options.   It may be tempting to hack into the ghosted version of the “Post.aspx” file in the 14-hive to just add the ratings control with markup, but then you know NOT to do that for many many reasons.

 

Here’s my happy workaround with two code-based steps…. If you don’t code, or you can’t deploy a solution, look elsewhere.

Step 0:  Enable ratings on the Posts list for every Blog site you create.  This will require Enterprise features to be activated.

Step 1:  Create a user control that will display a context-sensitive ratings control.  Beyond the scope of this posting, but it is quite do-able to create a user control with a Microsoft.SharePoint.Portal.WebControls.AverageRatingFieldControl embedded that you can scope to an individual blog post.

Step 2:  Wire this control to any and all “Post.aspx” pages in your farm.

Here’s the nitty gritty on this one.  I was able to pinpoint all instances of the “Post.aspx” within my farm by adding a little routine in code-behind of my site master page:

  1: #region Blog Wire

  2: /// <summary>

  3: /// Wire in ratings controls for the blog POST.ASPX file

  4: /// </summary>

  5: protected void BlogWire()

  6: {

  7:   if (SPContext.Current.Web.WebTemplateId == 9)

  8:   {

  9:     if (Request.FilePath.EndsWith("Post.aspx"))

 10:     {

 11:       string _ascxPath = @"~/_CONTROLTEMPLATES/Project.Web/BlogPostRating.ascx";

 12:       BlogPostRating thisBlogRating = (BlogPostRating)LoadControl(_ascxPath);

 13:       MSO_ContentDiv.Controls.AddAt(1, thisBlogRating);

 14:     }

 15:   }

 16: }

 17: #endregion

Break-down:

This code is called from the Page_Load event.

Line 5:  Checks the site’s web template ID.  #9 is the Blog web template.

Line 7:  Checks the file path ending with “Post.aspx”.

Lines 11-12:  Creates an instance of the control.

Line 13:  Wires in the control in the “MSO_ContentDiv” server control declared as runat=”server” from the master page.

 

Check out your embedded ratings control on Blog posts (see arrow below):

blogratings