Sunday, May 6, 2012

Read SharePoint Service using JQuery


I had spent a day to know that soap response can be read at the event complete but not success.
In success event soapresponse(data.responseXML) is always empty. I'm not sure if its a sepcial case with soap.

Below function of Jquery retrieves the SocialTag count of a specific URL from all users.
Complete event triggers all the time after the service call resulting in success or error. So i used a flag based on which i can get to know if the service call is success or error.

function GetLikeCountFromService(domId, url) {

var tagName = "I like it";
var queryStatus = true;

var soapEnv =
"<soap:Envelope xmlns:xsi='' xmlns:xsd='' xmlns:soap=''>\
<soap:Body> \
<GetTagTermsOnUrl xmlns=''>\
<url>" + url + "</url>\
<maximumItemsToReturn>'100</maximumItemsToReturn> \
</GetTagTermsOnUrl> \
</soap:Body> \

url: '/_vti_bin/socialdataservice.asmx',
type: 'POST',
dataType: "xml",
data: soapEnv,
complete: function (data) {

//process only if the query is success
if (queryStatus) {
var likeCount = 0;
$(data.responseXML).find("SocialTermDetail").each(function () {
var term = $(this).find("Term").find("Name").text();
if (term == tagName) {
likeCount = $(this).find("Count").text();
// set the like count

error: function (all, textStatus, errorThrown) { queryStatus = false; alert(errorThrown); },
contentType: "text/xml; charset=\"utf-8\""

JQuery made life easy with flexible API. I have implemented another function similar to it that will like a URL, in sharepoint terms tagging a url with "I Like It". As a combination this will work as similar to the Social Like feature in Facebook.


No comments:

Post a Comment