Techbubbles

SharePoint Social Data using Java Script

This post shows the code to call the default like button functionality in SharePoint 2010 and also shows you how to show the number of likes for given URL using Jquery and SPServices. Call the below javascript function in sharepoint socialdata.js file for liking an item. Below code can be used on an image click to replicate the SharePoint 2010 like

   1: function AddCompanyQuickTag(URL, T)

   2:  {

   3:     SafeRunFunction(function () {

   4:     var o = new SocialQuickTag("ClientID");

   5:     o.url = URL;

   6:     o.title = T; //document.title;

   7:     o.tagged_text = 'I Like It';

   8:     o.AddQuickTag(0);

   9:     }, 'SocialData.js', 'SocialQuickTag');

  10:     alert( T + ' page liked successfully');

  11:     }

To display the number of likes for an URL you can use the JQUERY and jquery library spservices which can downloaded from here  http://spservices.codeplex.com/releases/view/81401  The documentation on calling methods can be found here http://msdn.microsoft.com/en-us/library/ee590739.aspx

Refer the below javascript files in the page where you want to show the like count

<script language="javascript" type="text/javascript" src="/_layouts/SocialWebPart/Scripts/jquery-1.4.4.min.js"></script>

<script language="javascript" type="text/javascript" src="/_layouts/SocialWebPart/Scripts/jquery.SPServices-0.7.2.js"></script>

   1: function LoadCounter(URL) {

   2:        $(document).ready(function () {

   3:           alert(URL);

   4:        $().SPServices({

   5:        operation: "GetTagTermsOnUrl",

   6:        url:  URL ,

   7:        completefunc: function (xData, Status) {

   8:   $(xData.responseXML).find("SocialTermDetail").each(function () {

   9:    var counter = $("Count", $(this)).text();

  10:                 });

  11:   $("#counttags").html(counter);

  12:              }

  13:      });

  14:     });

  15:     }

countertags in above code is div  tag id where you show the number of likes.

Share this post :


Related Posts:

%d bloggers like this: