aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Get Number of Facebook likes, Shares, Comments Count for Url or Website

Jan 28, 2013

Here I will explain how to get number of
facebook likes count for url, get number of facebook shares count for url, get number of facebook comments count for url and number of facebook click count for url or website using json jQuery in using C# and VB.NET.

In previous articles I explained
Add multiple markers to google maps using JSON jQuery, jQuery UI autocomplete textbox with database, jQuery Increase or Decrease font size of website, Redirect to another page after some time delay and many articles relating to JQuery and Now I will explain how to get number facebook likes for url, number of facebook shares count, number of facebook comments count and number of facebook clicks count for url using json jQuery in

To implement this functionality we need to write the code like as shown below

<html xmlns="">
<title>Get facebook shares, comments, likes count of urls</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).ready(function() {
$('#btnurl').click(function() {
var url = $('#txturl').val();
document.getElementById('tbDetails').style.display = 'block';
type: "POST",
url: "WebService.asmx/BindDatatable",
data: "{urltxt:'" + url + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#tbDetails").append("<tr><td>" + data.d[i].Url + "</td><td>" + data.d[i].SharedCount + "</td><td>" + data.d[i].LikeCount + "</td><td>" + data.d[i].CommentCount + "</td><td>" + data.d[i].ClickCount + "</td><td>" + data.d[i].TotalCount + "</td></tr>");
error: function(result) {
<form id="form1" runat="server">
<td><b>Enter Url:</b></td>
<td><input type="text" id="txturl" /> </td>
<td><input type="button" id="btnurl" value="Get Url Count" /> </td>
<table id="tbDetails" cellpadding="1" cellspacing="1" style="border:solid 1px #000000; display:none">
<thead style="background-color:#DC5807; color:White; font-weight:bold">
<td>Shared Count</td>
<td>Likes Count</td>
<td>Comments Count</td>
<td>Clicks Count</td>
<td>Total Count</td>
If you observe above code in header section I mentioned url field as “WebService.asmx/BindDatatable” this mean we are calling BindDatatable method from WebService.asmx webservice.

To creat this webservice right click on your application >> Select Add New Item >> select Web Service >> click OK
Once webservice created open code behind file and write the following code

C# Code

using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net;
using System.Web.Services;

/// <summary>
/// Summary description for AutoCompleteService
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
public class WebService : System.Web.Services.WebService {
public UrlDetails[] BindDatatable(string urltxt)
List<UrlDetails> details = new List<UrlDetails>();
WebClient web = new WebClient();
string url = string.Format(" url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" + urltxt + "'");
string response = web.DownloadString(url);
DataSet ds = new DataSet();
using (StringReader stringReader = new StringReader(response))
ds=new DataSet();
DataTable dt = ds.Tables["link_stat"];
foreach (DataRow dtrow in dt.Rows)
UrlDetails website = new UrlDetails();
website.Url = dtrow["url"].ToString();
website.LikeCount = dtrow["like_count"].ToString();
website.SharedCount = dtrow["share_count"].ToString();
website.CommentCount = dtrow["comment_count"].ToString();
website.ClickCount = dtrow["click_count"].ToString();
website.TotalCount = dtrow["total_count"].ToString();
return details.ToArray();
public class UrlDetails
public string Url { get; set; }
public string SharedCount { get; set; }
public string LikeCount { get; set; }
public string CommentCount { get; set; }
public string ClickCount { get; set; }
public string TotalCount { get; set; }
VB.NET Code:

Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports System.Net
Imports System.Web.Services

''' <summary>
''' Summary description for AutoCompleteService
''' </summary>
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<WebService([Namespace]:="")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Script.Services.ScriptService()> _
Public Class WebService2
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function BindDatatable(ByVal urltxt As String) As UrlDetails()
Dim details As New List(Of UrlDetails)()
Dim web As New WebClient()
Dim url As String = String.Format(" url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" & urltxt & "'")
Dim response As String = web.DownloadString(url)
Dim ds As New DataSet()
Using stringReader As New StringReader(response)
ds = New DataSet()
End Using
Dim dt As DataTable = ds.Tables("link_stat")
For Each dtrow As DataRow In dt.Rows
Dim website As New UrlDetails()
website.Url = dtrow("url").ToString()
website.LikeCount = dtrow("like_count").ToString()
website.SharedCount = dtrow("share_count").ToString()
website.CommentCount = dtrow("comment_count").ToString()
website.ClickCount = dtrow("click_count").ToString()
website.TotalCount = dtrow("total_count").ToString()
Return details.ToArray()
End Function
Public Class UrlDetails
Public Property Url() As String
Return m_Url
End Get
Set(ByVal value As String)
m_Url = value
End Set
End Property
Private m_Url As String
Public Property SharedCount() As String
Return m_SharedCount
End Get
Set(ByVal value As String)
m_SharedCount = value
End Set
End Property
Private m_SharedCount As String
Public Property LikeCount() As String
Return m_LikeCount
End Get
Set(ByVal value As String)
m_LikeCount = value
End Set
End Property
Private m_LikeCount As String
Public Property CommentCount() As String
Return m_CommentCount
End Get
Set(ByVal value As String)
m_CommentCount = value
End Set
End Property
Private m_CommentCount As String
Public Property ClickCount() As String
Return m_ClickCount
End Get
Set(ByVal value As String)
m_ClickCount = value
End Set
End Property
Private m_ClickCount As String
Public Property TotalCount() As String
Return m_TotalCount
End Get
Set(ByVal value As String)
m_TotalCount = value
End Set
End Property
Private m_TotalCount As String
End Class

End Class

In case if you want to use this to count of multiple urls you need to replace url in webservice like as shown below

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

hiiii,suresh sir......):
I have three user Admin,Distributor and consumer..............
how could I redirect the page to repective users.
and your r giving 3-Tier architecture
in this example which database table r u created.........plz maintaion
in comments
i understand this example but which db table r u created i don't
how to redirect the page to repective users & how can i do setting this in web.config file........
plz give registration page And Login page with database tables in 3 tire architecture.....

Suresh Dasari said...

Check these articles for create roles and everything

Prateek Diwakar said...

Is there any code in to copy a text box code to clipboard for pasting another place ??

If Possible than Reply At

Thanks !!!

Anonymous said...

Nice article but sample code is NOT attached. Can you fix this please, thanks. :)

Suresh Dasari said...

Sample Code attached please check it once...

rams said...

nice sirrrr..

Anonymous said...

Thanx suresh sir.........
4 giving articles for create roles ......) much


this facebook shsres, likes count article not working for me, its showes error, Internal server error, what i need to do before i run this task.

Suresh Dasari said...

@Anil Kumar Reddy....
please check whether you have internet connection or not because dynamically we are getting contact details from facebook for that reason internet connection is necessary...

aditya kumar said...

im getting the wrong count, for ex this url " in facebook has 773 likes 64 comments but im getting the count as 105 count and 130 comments

Anonymous said...

im getting the wrong count, for ex this url "" in facebook has around 773 likes and 64 comments but im getting the count as

aditya kumar said...

ur page on facebook has 4373 likes but with this we are getting only 152 likes count

Suresh Dasari said...

@Aditya kumar...

4373 likes is my facebook page likes count not url count. 153 likes count is my website url count....

aditya kumar said...

sorrry to ask but, wat is url like and how is it different from facebook page like, if i want to like the url of this page, where should i click?

aditya kumar said...

also do u have any idea or source code for integrating website with facebook, like i should be able to post to my facebook wall from my website or some thing that makes me to interact with facebook from my page?

Anonymous said...

when i run the code it generates only html table header and shows "Error" in alert box, what can be the cause behind it ??

Mohan Kumar said...

i want to know Multilingual web applications and how it is work...

Get Facebook Likes said...

Its simply great piece of information, I was really unaware of such information. I would really like to know some more information regarding same.

Anonymous said...

it's showing the count on my development machine but not on the server. any idea please.

Rocky said...

Hi I want all comment not the comment count of facebook page.

Anonymous said...

Facebook to tame j hack kri nakhyu...
Hve url pr thi username & password pn aapi do.

Anonymous said...

Hi,,,, i have small doubt .... How its returning the count while entering the url of our website ???

kanchana krishnaswamy said...

is this code gives how many likes i got for my face book page or ?

Azhar Ansari said...

suresh sir can u help me about post message on facebook wall using windows service(means schedule post).

Anonymous said...

Hello, nice article. I am able to implement this but could you please tell me how to get LinkedIn stats as well? I mean a table which shows both facebook and linkedin stats for a url

Anonymous said...

Hello, can you provide the updated version of the code as per the latest API used. Also i want to update the number of likes in facebook from my application. Suggest me how to achieve the same using .NET.

naveen verma said...

sir this code is not work because facebook block this api .plz sir guide me how its work for this time

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.