Tom Clarkson

SharePoint, Startups and some other stuff

SharePoint Blog Content Rating with JavaScript and Web Services

with 7 comments

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/“>

Ratings

[postId]

   

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.

Advertisement

Written by Tom Clarkson

September 24, 2007 at 5:00 pm

Posted in SharePoint

7 Responses

Subscribe to comments with RSS.

  1. hi tom,
    i read your post about SharePoint Blog Content Rating with JavaScript and Web Services.
    i was exactly trying out something like this. but in your post i couldnt make out how to implement,
    i am a beginner in sharepoint so can you give me a link or reply which would help to create a sample.
    thanks in advance

    unnikrishnan

    February 19, 2008 at 1:48 am

  2. There’s a good walkthrough of this technique on Colin Byrne’s blog: http://blogs.flexnetconsult.co.uk/colinbyrne/2006/04/01/CrossBrowserAJAXForSharePointListsPart1.aspx

    It’s not specific to doing ratings of course, but it gives a good intro to manipulating sharepoint data from javascript.

    Tom Clarkson

    February 24, 2008 at 4:19 am

  3. I am unable to figure out error in below code – please help
    `__abENT__lt;?xml version=__abENT__quot;1__abENT__#46;0__abENT__quot; encoding=__abENT__quot;utf-8__abENT__quot; ?__abENT__gt;
    - __abENT__lt;soap:Envelope xmlns:xsi=__abENT__quot;http:__abENT__#8260;__abENT__#8260;www__abENT__#46;w3__abENT__#46;org__abENT__#8260;2001__abENT__#8260;XMLSchema-instance__abENT__quot; xmlns:xsd=__abENT__quot;http:__abENT__#8260;__abENT__#8260;www__abENT__#46;w3__abENT__#46;org__abENT__#8260;2001__abENT__#8260;XMLSchema__abENT__quot; xmlns:soap=__abENT__quot;http:__abENT__#8260;__abENT__#8260;schemas__abENT__#46;xmlsoap__abENT__#46;org__abENT__#8260;soap__abENT__#8260;envelope__abENT__#8260;__abENT__quot;__abENT__gt;
    - __abENT__lt;soap:Body__abENT__gt;
    - __abENT__lt;GetListItems xmlns=__abENT__quot;http:__abENT__#8260;__abENT__#8260;schemas__abENT__#46;microsoft__abENT__#46;com__abENT__#8260;sharepoint__abENT__#8260;soap__abENT__#8260;__abENT__quot;__abENT__gt;
    __abENT__lt;listName__abENT__gt;User-Project__abENT__lt;__abENT__#8260;listName__abENT__gt;
    - __abENT__lt;query__abENT__gt;
    - __abENT__lt;Query__abENT__gt;
    - __abENT__lt;Where__abENT__gt;
    - __abENT__lt;Eq__abENT__gt;
    __abENT__lt;FieldRef Name=__abENT__quot;ProjectName__abENT__quot; __abENT__#8260;__abENT__gt;
    __abENT__lt;Value Type=__abENT__quot;Text__abENT__quot;__abENT__gt;Project2__abENT__lt;__abENT__#8260;Value__abENT__gt;
    __abENT__lt;__abENT__#8260;Eq__abENT__gt;
    __abENT__lt;__abENT__#8260;Where__abENT__gt;
    __abENT__lt;__abENT__#8260;Query__abENT__gt;
    __abENT__lt;__abENT__#8260;query__abENT__gt;
    - __abENT__lt;ViewFields__abENT__gt;
    __abENT__lt;FieldRef Name=__abENT__quot;ProjectSiteLink__abENT__quot; __abENT__#8260;__abENT__gt;
    __abENT__lt;FieldRef Name=__abENT__quot;ProjectName__abENT__quot; __abENT__#8260;__abENT__gt;
    __abENT__lt;__abENT__#8260;ViewFields__abENT__gt;
    __abENT__lt;__abENT__#8260;GetListItems__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:Body__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:Envelope__abENT__gt;
    `

    ruchi

    June 11, 2009 at 4:29 am

  4. This code example saved my day. Actually, night :) Thanks!

    Ostap

    July 30, 2009 at 12:00 pm

  5. THANK YOU SO MUCH! I’ve been pulling my hair out trying to figure out why my query wasn’t filtering – and turns out I needed the double query thing you mentioned… Thanks so much for posting this (and figuring it out!)

    Adge

    November 19, 2009 at 8:38 am

  6. Hi Tom,
    Please suggest us onthe below GetListItems xml that is ue for fetch the ID of the Case number from the Share Point List.

    Whenevr I Query the Share point webservice with SOAP UI , I get a Bad request error .Please suggest .I am not bale to figure out where I am going wrong .
    `__abENT__lt;?xml version=__abENT__quot;1__abENT__#46;0__abENT__quot; encoding=__abENT__quot;utf-8__abENT__quot;?__abENT__gt;
    __abENT__lt;soap:Envelope xmlns:xsi=__abENT__quot;http:__abENT__#8260;__abENT__#8260;www__abENT__#46;w3__abENT__#46;org__abENT__#8260;2001__abENT__#8260;XMLSchema-instance__abENT__quot; xmlns:xsd=__abENT__quot;http:__abENT__#8260;__abENT__#8260;www__abENT__#46;w3__abENT__#46;org__abENT__#8260;2001__abENT__#8260;XMLSchema__abENT__quot; xmlns:soap=__abENT__quot;http:__abENT__#8260;__abENT__#8260;schemas__abENT__#46;xmlsoap__abENT__#46;org__abENT__#8260;soap__abENT__#8260;envelope__abENT__#8260;__abENT__quot;__abENT__gt;
    __abENT__lt;soap:Body__abENT__gt;
    __abENT__lt;GetListItems xmlns=__abENT__quot;http:__abENT__#8260;__abENT__#8260;schemas__abENT__#46;microsoft__abENT__#46;com__abENT__#8260;sharepoint__abENT__#8260;soap__abENT__#8260;__abENT__quot;__abENT__gt;
    __abENT__lt;soap:GetListItems__abENT__gt;
    __abENT__lt;soap:listName__abENT__gt;{0911DDEB-C23B-4815-AFB8-7610462E9D11}__abENT__lt;__abENT__#8260;soap:listName__abENT__gt;
    __abENT__lt;query__abENT__gt;
    __abENT__lt;Query__abENT__gt;
    __abENT__lt;Where__abENT__gt;
    __abENT__lt;Eq__abENT__gt;
    __abENT__lt;FieldRef Name=__abENT__apos;ID__abENT__apos; __abENT__#8260;__abENT__gt;
    __abENT__lt;Value Type=__abENT__apos;Number__abENT__apos;__abENT__gt;12__abENT__lt;__abENT__#8260;Value__abENT__gt;
    __abENT__lt;__abENT__#8260;Eq__abENT__gt;
    __abENT__lt;__abENT__#8260;Where__abENT__gt;
    __abENT__lt;__abENT__#8260;Query__abENT__gt;
    __abENT__lt;__abENT__#8260;query__abENT__gt;
    __abENT__lt;soap:viewFields__abENT__gt;
    __abENT__lt;FieldRef Name=__abENT__quot;ID__abENT__quot; __abENT__#8260;__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:viewFields__abENT__gt;
    __abENT__lt;soap:rowLimit__abENT__gt;1__abENT__lt;__abENT__#8260;soap:rowLimit__abENT__gt;
    __abENT__lt;soap:queryOptions__abENT__gt;
    __abENT__lt;IncludeMandatoryColumns__abENT__gt;FALSE__abENT__lt;__abENT__#8260;IncludeMandatoryColumns__abENT__gt;__abENT__lt;DateInUtc__abENT__gt;TRUE__abENT__lt;__abENT__#8260;DateInUtc__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:queryOptions__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:GetListItems__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:Body__abENT__gt;
    __abENT__lt;__abENT__#8260;soap:Envelope__abENT__gt;
    `

    Regards,
    Sabih

    Anwer

    December 8, 2009 at 1:40 am

  7. “if you have a site getting that much usage you probably have sufficient access to do something better server side. ”

    yeah, i do. drupal. god, i hate sharepoint. it’s the worst CMS/document managent/thing ever. even phpnuke was better.

    October 28, 2010 at 2:26 am


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.