Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Website’s Shared Count on Social Network

4.25/5 (4 votes)
13 Nov 2011CPOL1 min read 22.7K  
Website’s Shared Count on Social Network

Introduction


Social Networking Sites are widely used in Internet World. Facebook and Twitter are one of leading sites amongst these. Every day globally millions of users keep posting their stuff on these sites.

Here is a trick to check how many times your website has been shared on Facebook and Twitter.


Check Shared Count on Facebook


Facebook API



Facebook provides below REST based API, which returns information of your website’s “Shared Count” in JSON format.

"http://graph.facebook.com/?id=WebSiteUrl"


Consuming Facebook API



[Please refer “btnShareCount_Click” event of “SocialShareCount.aspx.cs” Page for better understanding of code.]


//REST API Url of Facebook for Website Share Count Information. 
 string fbRestAPIUrl = "http://graph.facebook.com/?id=";
                    
 //Append Website Url
 fbRestAPIUrl = fbRestAPIUrl + txtWebsite.Text;

 string fbSharingInformationJson=GetSharingInformationJson(fbRestAPIUrl);

 FaceBookSharingInfo oFaceBookSharingInfo = (FaceBookSharingInfo)Newtonsoft.Json.JsonConvert.DeserializeObject(fbSharingInformationJson, typeof(FaceBookSharingInfo));
                    
 lblWebsite.Text = oFaceBookSharingInfo.id;
 lblShareCount.Text = oFaceBookSharingInfo.shares.ToString();

Method “GetSharingInformationJson” will call REST API and will return information in JSON format.

We are then desirializing this JSON string to object of type “FaceBookSharingInfo” Class.

Implementation of method “GetSharingInformationJson” is as below:


private string GetSharingInformationJson(string restApiUrl)
{
  string output=string.Empty;
  Uri address = new Uri(restApiUrl);
 
  // Create the web request 
  System.Net.HttpWebRequest request = System.Net.WebRequest.Create(address) as    System.Net.HttpWebRequest;
  request.Method = "GET";
  request.ContentType = "text/json";
 
  using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
  {
    // Get the response stream 
    System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
 
    output = reader.ReadToEnd();
  }

  return output;
}

Definition of Class “FaceBookSharingInfo“ is as below:


public class FaceBookSharingInfo
{
  public string id
  {
    get;
    set;
  }

  public int shares
  {
    get;
    set;
  }

  public int comments
  {
    get;
    set;
  }
}

What will be the Result?


Run the attached application. Enter “Website Url”, Select “Social Networking Site” to “Facebook” and click on “Show Share Count” Button.



Check Shared Count on Twitter


Twitter API


Twitter provides below REST based API, which returns information of your website’s “Shared Count” in JSON format.

“http://urls.api.twitter.com/1/urls/count.json?url=WebsiteUrl”


Consuming Twitter API


[Please refer to the “btnShareCount_Click” event of “SocialShareCount.aspx.cs” Page for better understanding of code.]


//REST API Url of Twitter for Website Share Count Information. 
string twRestAPIUrl = "http://urls.api.twitter.com/1/urls/count.json?url=";

//Append Website Url
twRestAPIUrl = twRestAPIUrl + txtWebsite.Text;

string twSharingInformationJson = GetSharingInformationJson(twRestAPIUrl);

TwitterSharingInfo oTwitterSharingInfo = (TwitterSharingInfo)Newtonsoft.Json.JsonConvert.DeserializeObject(twSharingInformationJson, typeof(TwitterSharingInfo));

lblWebsite.Text = oTwitterSharingInfo.url;
lblShareCount.Text = oTwitterSharingInfo.count.ToString();

We have seen implementation of method “GetSharingInformationJson” in Facebooks section above.

Definition of Class “TwitterSharingInfo” as below:


public class TwitterSharingInfo
{
  public string url
  {
    get;
    set;
  }

  public int count
  {
    get;
    set;
  }
}

What will be the Result?


Run the attached application. Enter “Website Url”, Select “Social Networking Site” to “Twitter” and click on “Show Share Count” Button.




I hope this was interesting.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)