Click here to Skip to main content
12,700,567 members (30,231 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.5K views
5 bookmarked
Posted

Website’s Shared Count on Social Network

, 13 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


https://www.codeproject.com/script/Membership/Uploads/3873871/4455323-FacebookResult.png

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.


https://www.codeproject.com/script/Membership/Uploads/3873871/4455323-TwitterResult.png

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)

Share

About the Author

RaisKazi
Technical Lead
United States United States
Full Stack Technology Professional

Hands-on experience with -
. Asp.Net, MVC, C#, Ajax
. JavaScript, Jquery, AngularJS
. Xamarin
. SharePoint
. VBScript

Tested flavors of -
. Java, Perl, Python, NodeJS, Android, IOS(little bit)

You may also be interested in...

Pro

Comments and Discussions

 
Questionnice 1 Pin
idris aliu19-Apr-12 2:00
memberidris aliu19-Apr-12 2:00 
AnswerRe: nice 1 Pin
RaisKazi19-Apr-12 2:49
memberRaisKazi19-Apr-12 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 13 Nov 2011
Article Copyright 2011 by RaisKazi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid