Tom Clarkson is a SharePoint consultant and entrepreneur based in Sydney, Australia.

Contact Details

Links



Recent Searches



Archives




Past Posts







RSS Feed

SharePoint Blog Content Rating with JavaScript and Web Services

SharePoint 2007
Monday September 24 2007

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.

   

   

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">

<listName>Ratings</listName>

<viewName></viewName>

<queryOptions></queryOptions>

<query><Query>

<Where>

<Eq>

<FieldRef Name="PostID"/>

<Value Type="Text">[postId]</Value>

</Eq>

</Where>

</Query></query>

</GetListItems>

</soap:Body>

</soap:Envelope>

   

Note the <query><Query> 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.

Comments

On 18 Feb 2008 06:48, unnikrishnan said:
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
On 23 Feb 2008 09:19, Tom Clarkson said:
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.
On 10 Jun 2009 09:29, ruchi said:
I am unable to figure out error in below code - please help

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <listName>User-Project</listName> 
- <query>
- <Query>
- <Where>
- <Eq>
  <FieldRef Name="ProjectName" /> 
  <Value Type="Text">Project2</Value> 
  </Eq>
  </Where>
  </Query>
  </query>
- <ViewFields>
  <FieldRef Name="ProjectSiteLink" /> 
  <FieldRef Name="ProjectName" /> 
  </ViewFields>
  </GetListItems>
  </soap:Body>
  </soap:Envelope>
On 30 Jul 2009 05:00, Ostap said:
This code example saved my day. Actually, night :) Thanks!
On 05 Nov 2009 11:13, DAVETİYE said:
güzel davetiye sözleri ve davetiye modelleri
On 06 Nov 2009 11:57, davetiye said:
güzel davetiye sözleri ve davetiye metinleri
On 19 Nov 2009 01:38, Adge said:
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!)
On 07 Dec 2009 06:40, Anwer said:
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 .

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <soap:GetListItems>
         <soap:listName>{0911DDEB-C23B-4815-AFB8-7610462E9D11}</soap:listName>
       <query>
	<Query>
           <Where>
                <Eq>
                    <FieldRef Name='ID' />
                    <Value Type='Number'>12</Value>
                </Eq>
            </Where>
	</Query>       
  	</query>
       <soap:viewFields>
            <FieldRef Name="ID" />
         </soap:viewFields>
        <soap:rowLimit>1</soap:rowLimit>
         <soap:queryOptions>
            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>
         </soap:queryOptions>
      </soap:GetListItems>
</soap:Body>
</soap:Envelope>



Regards,
Sabih
On 07 Dec 2009 06:42, Anwer said:
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 .

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <soap:GetListItems>
         <soap:listName>{0911DDEB-C23B-4815-AFB8-7610462E9D11}</soap:listName>
       <query>
	<Query>
           <Where>
                <Eq>
                    <FieldRef Name='ID' />
                    <Value Type='Number'>12</Value>
                </Eq>
            </Where>
	</Query>       
  	</query>
       <soap:viewFields>
            <FieldRef Name="ID" />
         </soap:viewFields>
        <soap:rowLimit>1</soap:rowLimit>
         <soap:queryOptions>
            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>
         </soap:queryOptions>
      </soap:GetListItems>
</soap:Body>
</soap:Envelope>



Regards,
Sabih
On 07 Dec 2009 08:48, Anwer said:
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 .

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <soap:GetListItems>
         <soap:listName>{0911DDEB-C23B-4815-AFB8-7610462E9D11}</soap:listName>
       <query>
	<Query>
           <Where>
                <Eq>
                    <FieldRef Name='ID' />
                    <Value Type='Number'>12</Value>
                </Eq>
            </Where>
	</Query>       
  	</query>
       <soap:viewFields>
            <FieldRef Name="ID" />
         </soap:viewFields>
        <soap:rowLimit>1</soap:rowLimit>
         <soap:queryOptions>
            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>
         </soap:queryOptions>
      </soap:GetListItems>
</soap:Body>
</soap:Envelope>



Regards,
Sabih
On 07 Dec 2009 09:26, Anwer said:
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 .

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <soap:GetListItems>
         <soap:listName>{0911DDEB-C23B-4815-AFB8-7610462E9D11}</soap:listName>
       <query>
	<Query>
           <Where>
                <Eq>
                    <FieldRef Name='ID' />
                    <Value Type='Number'>12</Value>
                </Eq>
            </Where>
	</Query>       
  	</query>
       <soap:viewFields>
            <FieldRef Name="ID" />
         </soap:viewFields>
        <soap:rowLimit>1</soap:rowLimit>
         <soap:queryOptions>
            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>
         </soap:queryOptions>
      </soap:GetListItems>
</soap:Body>
</soap:Envelope>



Regards,
Sabih
On 07 Dec 2009 09:27, Anwer said:
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 .

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"> 
      <soap:GetListItems>
         <soap:listName>{0911DDEB-C23B-4815-AFB8-7610462E9D11}</soap:listName>
       <query>
	<Query>
           <Where>
                <Eq>
                    <FieldRef Name='ID' />
                    <Value Type='Number'>12</Value>
                </Eq>
            </Where>
	</Query>       
  	</query>
       <soap:viewFields>
            <FieldRef Name="ID" />
         </soap:viewFields>
        <soap:rowLimit>1</soap:rowLimit>
         <soap:queryOptions>
            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>
         </soap:queryOptions>
      </soap:GetListItems>
</soap:Body>
</soap:Envelope>



Regards,
Sabih

Leave a comment