ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
Posted by in CSharp February 21, 2012 5 Comments

The following displays how to get total comments, comment count, total likes, like count, total shares, share count, total count, click count, comment box count of one or more URL that are shared or integrated with Facebook by using ASP.NET (C#).

This article includes getting Facebook Share Statistics result (in XML format), parsing the out put string and displaying on the browser.

At the end, I also provide the full source code and a demonstration link for you to play with.

ASP.NET (C#) Get Facebook Share Statistics – Total Comments, Total Likes, Total Shares

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
 
public partial class aspx_get_total_comments_total_likes_total_shares_of_facebook_url : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (txtURLs.Text == "")
                txtURLs.Text = "http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/";
        }
    }
 
    public string get_web_content(string url)
    {
        Uri uri = new Uri(url);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Get;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string output = reader.ReadToEnd();
        response.Close();
 
        return output;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string request_url = txtURLs.Text.Trim();
        request_url = Server.UrlEncode(request_url);
        request_url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" + request_url;
 
        lblRequestURL.Text = request_url;
        hplRawResult.NavigateUrl = request_url;
 
        string Facebook_raw_data = get_web_content(request_url);
 
        XmlDocument dom = new XmlDocument();
        dom.LoadXml(Facebook_raw_data);
 
        XmlNodeList root = dom.GetElementsByTagName("link_stat");
 
        string result = "";
 
        foreach (XmlNode node in root)
        {
            XmlElement companyElement = (XmlElement)node;
 
            result += "<b>URL: </b>" + companyElement.GetElementsByTagName("url")[0].InnerText + "<br>";
            result += "<b>share_count: </b> " + companyElement.GetElementsByTagName("share_count")[0].InnerText + "<br>";
            result += "<b>like_count: </b>: " + companyElement.GetElementsByTagName("like_count")[0].InnerText + "<br>";
            result += "<b>comment_count: </b>: " + companyElement.GetElementsByTagName("comment_count")[0].InnerText + "<br>";
            result += "<b>total_count: </b>: " + companyElement.GetElementsByTagName("total_count")[0].InnerText + "<br>";
            result += "<b>click_count: </b>: " + companyElement.GetElementsByTagName("click_count")[0].InnerText + "<br>";
            result += "<b>commentsbox_count: </b>: " + companyElement.GetElementsByTagName("commentsbox_count")[0].InnerText + "<br>";
            result += "------------------------------------------<br>";
        }
 
        ltrResult.Text = result;
    }
}

In the CodeFile above:

  • 1. The get_web_content() function simply use ASP.NET Web Request to get the Facebook Share Statistics output in XML format from Facebook getStats link.
  • 2. When people click Submit button, it will encode the input URLs, get the XML output from Facebook then parse the data using XMLDocument.

Note: you should validate the input URLs in your application.

HTML of this page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aspx-get-total-comments-total-likes-total-shares-of-facebook-url.aspx.cs" Inherits="aspx_get_total_comments_total_likes_total_shares_of_facebook_url" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td valign="top" align="left">URLs: </td>
            <td align="left"><asp:TextBox runat="server" ID="txtURLs" Width="300px"></asp:TextBox> (separated by comma)<br />
+ http://4rapiddev.com<br />
+ http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/</td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Button runat="server" ID="btnSubmit" Text="Submit" 
                    onclick="btnSubmit_Click" /></td>
        </tr>
        <tr>
            <td colspan="2"><hr /></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Request URL</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:Label runat="server" ID="lblRequestURL"></asp:Label></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Raw result from Facebook</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:HyperLink runat="server" ID="hplRawResult" Text="Click here" Target="_blank"></asp:HyperLink></td>
        </tr>
        <tr>
            <td colspan="2"><strong>Facebook Share Statistic Information</strong></td>
        </tr>
        <tr>
            <td colspan="2"><asp:Literal runat="server" ID="ltrResult"></asp:Literal></td>
        </tr>
    </table>    
    </div>
    </form>
</body>
</html>

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • Kumar

    tried the above code but at the line “HttpWebResponse response = (HttpWebResponse)request.GetResponse”  throwing error “Unable to connect to the remote server”, wht could be wrong, do we need to pass any credentials to retrieve the data, if so how?

    • http://4rapiddev.com/ Hoan Huynh

      It seems that your server is stand behind a firewall, please ask your network administrator about that.

  • Mrconsistent 26

    Hi your article is great…..
    I have one query i want to store individual details of users who likes my posts or comments on it……..

    • http://4rapiddev.com/ Hoan Huynh

      Could you share it?

      Thanks,
      H2.

  • jawahar

    super