Get Details of Facebook Account in Web API

Introduction

This article explains how to get the number of shared, number of likes, number of comments, number of clicks and the total number of counts of a Facebook account.

Use the following procedure to create a sample application.

Step 1

Create an application as in the following:

  • Start Visual Studio 2012.
  • From the Start window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

    Select MVC4 Application

  • From the "MVC4 Project" window select "Web API".

    Select Web API

  • Click on the "OK" button.

Step 2

Create a Model Class as in the following:

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select class.

    Add Model Class

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace RistricedDate.Models  
  6. {  
  7.     public class FaceBookModel  
  8.     {  
  9.         public string Url { getset; }  
  10.         public string No_of_Shared { getset; }  
  11.         public string No_of_Likes { getset; }  
  12.         public string No_of_Comment { getset; }  
  13.         public string No_of_Click { getset; }  
  14.         public string TotalNo { getset; }  
  15.     }  
  16. }  

Step 3

Now select the "HomeController".

  • In the "Solution Explorer".
  • Expand the Controller folder.
  • Select the "HomeController".

Add the folowing code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using RistricedDate.Models;  
  7. using System.Net;  
  8. using System.IO;  
  9. using System.Data;  
  10. namespace RistricedDate.Controllers  
  11. {  
  12.     public class HomeController : Controller  
  13.     {  
  14.         public ActionResult Index()  
  15.         {  
  16.             FaceBookModel objfbdetail = new FaceBookModel();  
  17.             WebClient web = new WebClient();  
  18.             string path = string.Format("https://api.facebook.com/method/fql.query?query=SELECT url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='c-sharpcorner.com'");  
  19.             string res = web.DownloadString(path);  
  20.             DataSet obj = new DataSet();  
  21.             using (StringReader stringReader = new StringReader(res))  
  22.             {  
  23.                 obj = new DataSet();  
  24.                 obj.ReadXml(stringReader);  
  25.             }  
  26.             DataTable ndt = obj.Tables["link_stat"];  
  27.             foreach (DataRow dtrow in ndt.Rows)  
  28.             {  
  29.                 objfbdetail.Url = dtrow["url"].ToString();  
  30.                 objfbdetail.No_of_Likes = dtrow["like_count"].ToString();  
  31.                 objfbdetail.No_of_Shared = dtrow["share_count"].ToString();  
  32.                 objfbdetail.No_of_Comment = dtrow["comment_count"].ToString();  
  33.                 objfbdetail.No_of_Click = dtrow["click_count"].ToString();  
  34.                 objfbdetail.TotalNo = dtrow["total_count"].ToString();  
  35.             }  
  36.             return View(objfbdetail);  
  37.         }  
  38.     }  
  39. }  

Step 4

Now write some HTML code in the "index.cshtml" file as in the following:

  • In the "Solution Explorer".

  • Expand "Views" folder.

  • Select "Home" -> "index.cshtml".

Add the following code:

  1. @model RistricedDate.Models.FaceBookModel  
  2. @{  
  3.     ViewBag.Title = "Get the Facebook detail of the Site";  
  4. }  
  5. <table width="50%" cellpadding="5" cellspacing="0" border="1">  
  6. <tr>  
  7. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">URL</td>  
  8. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">Shared</td>  
  9. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">Like</td>  
  10. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">Comment</td>  
  11. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">Click</td>  
  12. <td align="center" style="font-weight:bold;background-color:firebrick;color:yellow;">Total</td>  
  13. </tr>  
  14. <tr>  
  15. <td align="center">@Model.Url</td>  
  16. <td align="center">@Model.No_of_Shared</td>  
  17. <td align="center">@Model.No_of_Likes</td>  
  18. <td align="center">@Model.No_of_Comment</td>  
  19. <td align="center">@Model.No_of_Click</td>  
  20. <td align="center">@Model.TotalNo</td>  
  21. </tr>  
  22. </table>   

Step 5

Execute the application:

Display Facebook detail