Customized Content Rating for SharePoint
I wanted to share with you a recent client undertaking involving adding "star rating" and comments to SharePoint content. As usual, clients prefer not to build everything from scratch, so I looked what was available via 3rd party options and found 2 potentially viable solutions:
SharePoint Document Rating System from codeplex: http://www.codeplex.com/spdocrating and a licensed offering from KwizCom: http://www.kwizcom.com/
At the time, the codeplex solution seemed a little more difficult to deal with, requiring content types and multiple columns in a list to function. These column also appeared to be based on non-standard formats, and it was not straightforward to use the columns as managed properties to create more fine-tuned search options.
Since the KwizCom solution was simply a custom field type installed into SharePoint based on a common decimal base type, it seemed much easier to implement. The downside was that it was some custom code, not open source, and it was entirely obfuscated so when I needed to make some adjustments based on my requirements, I was really stuck with what they gave me. Here's what I did in case someone is thinking about doing this:
I added their custom field to my SharePoint environment and created a site column from it called Ratings. Below is a screen shot of what this looks like in a list/library
The above shows what the column looks like and what it shows you when you hover over the item. When you click on the item you get a pop up where you can rate and read comments similar to this:
Ok, so the easy part is done: Install the product and add it to a list, and fill out some data. They give you that for free :) Now I had to come up with my own solution for the next 2 problems. First, I needed to include the ratings for documents in search results. It turns out to be pretty easy, but there were a couple wrinkles.
1. I had to copy some images over from the KwizCom installation location to where the layout images live in the 12 hive here: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\IMAGES
2. Then I had to modify the Search Results page (Search Core Results) to include my column in the XML, and then add a series of XSL "when" to display the star rating image AND the link to the comments as this was a requirement.
<xsl:when test="contentrating='1'"> - <img align="top" style="cursor:pointer;" onclick="window.ratingelement=this.parentNode;commonShowModalDialog('/_layouts/RatingRedirect.aspx?ItemURL={url}&SiteName={sitename}&a=1' ,'dialogHeight:450px;dialogWidth:350px;scroll:no;toolbar:no;status:no;resizable:no;', null, this.parentNode);" src="_layouts/KWizCom_RatingSolution/Images/1.0.gif"/></xsl:when>
The savvy of you may look at this XSL and wonder what "RatingRedirect.aspx" is. Well, this is something I had to come up with because the KwizCom code is locked down. Essentially I had to write a gateway page that took the querystring data available in the search results page and send it to a location that had the ability to look up the proper data required by the KwizCom page to load rating details. The aspx page code I wrote looks like this:
public partial class RatingRedirect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ProcessRedirect();
}
private void ProcessRedirect()
{
string itemURL = Request.QueryString["ItemURL"];
string siteUrl = Request.QueryString["SiteName"];
string destination = Request.QueryString["a"];
using (SPSite mySite = new SPSite(siteUrl))
{
using (SPWeb myWeb = mySite.OpenWeb())
{
SPListItem item = myWeb.GetListItem(itemURL);
if (item != null)
{
if (destination == "1")
{
Response.Redirect(item.Web.Url + "/_layouts/KWizCom_RatingSolution/RatingPopup.aspx?itemId=" + item.ID + "&listId={" + item.ParentList.ID + "}");
}
else if (destination == "2")
{
Response.Redirect(item.Web.Url + "/_layouts/KWizCom_RatingSolution/CommentsPopup.aspx?itemId=" + item.ID + "&listId={" + item.ParentList.ID + "}");
}
}
else
{
Response.Write("<br> Sorry, this item cannot be rated.");
}
}
}
}
}
The basics here are that I need to some ListItem data that is completely unavailable at the search results page.
Also consider that only list items can contain ratings. Much of the unratable content can be exluded from search results using contentclass exclusions in your search scopes, but the one that tripped me up was a standard site aspx page. If the page did not live in a library but was returned in search results, it would display with zero ratings. When a user clicked on the details they would receive an error because this was an unratable item. You see my rudimentary error handling section in the code above to at least inform people about the status.
We also added a higher level check in the XSL to only return rating data from content sources that actually had the rating installed. Other SharePoint web applications or site collections within the SSP that does not contain rating data needs to be excluded.
These changes allowed my search results to show the ratings details AND allowed users to view the comments AND add their own comments right from the search results page. The only hiccup is that until another crawl is run, the changes won't be shown in the search results because that data is pulled from the index and not in real time. A small price to pay.
Finally, I had to deploy this. Essentially the client wanted to add this to any document libraries that already existed in their site collection. It seems every client requires me to write a tool that iterates over a site collection and checks for something in each web/list. It's either inheritance, permissions, features, etc. that need to be set for every list if some condition is met. So I had some code I could re-use. I won't paste the whole thing here, but it was pretty simple:
I iterated over every site and did the following: checked for each list to see if it inherited from base templates of document library or link lists. If so, I iterated over the fields to see if the Rating field was present. If not, I would add the field to the list, then add it to the default view of the list.
Then you have to "tickle" the list, and un-tickle it. This one was critical. Due to the nature in which this data is stored, there is an inherent bug with the KwizCom solution. Their design created a hidden list per web that holds the rating data. This list is not created until a least one rating is made in the web. This code must not run with elevated permissions because users without proper permissions to create the list will receive an error. It's odd that they missed this because their solution only requires read access in order to rate items, so they must have written elevated permission code elsewhere in their solution.
At any rate, the tickle adds a rating to the first item in the list, updates the item, then removes the rating. This ensures the container list exists moving forward.
All in all it did not take too long to implement and the 3rd party price was pretty inexpensive.
I hope this helps,
-Ryan Thomas