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.]
string fbRestAPIUrl = "http://graph.facebook.com/?id=";
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);
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)
{
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.]
string twRestAPIUrl = "http://urls.api.twitter.com/1/urls/count.json?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.