Another feature I’ve been looking at lately is letting users rate content posted to a SharePoint blog. I’ve seen it done before, but sometimes running custom code on the server isn’t an option, so I’ve been working on an implementation that doesn’t require anything special server side.

Most things you are likely to want to do with SharePoint can be done through the standard web services, and therefore can be done entirely with JavaScript. In the case of content rating two web service calls will be needed - one to get the existing rating and one to add a new rating.

On the server, all I need to set up is a ratings list with two columns - PostID and Rating. Users will need contribute access to the list.

The first part of the implementation (after adding some test data to the ratings list) is to retrieve all the ratings for a specific post.

Making the request itself is reasonably easy :

xmlhttp.open("POST", "/_vti_bin/Lists.asmx",true); 
xmlhttp.setRequestHeader("Content-Type","text/xml; charset=utf-8"); 
xmlhttp.setRequestHeader("SOAPAction","http://schemas.microsoft.com/sharepoint/soap/GetListItems"); 
xmlhttp.setRequestHeader("Content-Length", xmlrequest.length); 
xmlhttp.onreadystatechange=GetRatingsHandler; 
xmlhttp.send(xmlrequest); 

The tricky part is getting the value of xmlrequest right - the documentation for the web service isn’t much help.

<getListItems xmlns="[http://schemas.microsoft.com/sharepoint/soap/](http://schemas.microsoft.com/sharepoint/soap/)">

Ratings

[xml lost in some long ago blog migration]

Note the bit - as wrong as it looks it needs to be like that - if at all different you just get an unfiltered list back - no useful errors, it just ignores the query. It was quite frustrating getting that bit worked out, especially when the other parameters required a bit of trial and error as well. After that it’s just a matter of writing a simple javascript function to add up and display the ratings returned.

Adding a new rating is easy once you have GetListItems working. The documentation for Lists.UpdateListItems on MSDN is reasonably clear on actual usage.

This approach obviously isn’t going to scale to millions of ratings, but if you have a site getting that much usage you probably have sufficient access to do something better server side.

The other issue with using JavaScript in this way is that it won’t work for anonymous access, which is why there isn’t a demo on this post. To enable anonymous access you’ll need a custom web service that doesn’t require any credentials. JavaScript to the out of box web services does however work well with internal blogs where everyone is authenticated, which happens to be the sort of environment where you are more likely to have trouble getting code onto the server.