ARTICLE

Facebook RSS Feed Reader in MVC 4

Posted by Abhimanyu K Vatsa Articles | ASP.NET MVC with C# December 04, 2012
In this article you will learn how to setup the Facebook RSS Feed Reader in MVC 4.
Reader Level:

In this article you will learn how to setup the Facebook RSS Feed Reader in MVC 4. Let's look at an image of what we will be creating in this article; see:




Where to find the RSS feed of my wall?


To find the RSS feed for you wall, login to your Facebook account and then navigate to the URL https://www.facebook.com/notification. In the top you will find a link to open the RSS feed page.




Once you are there, you will notice the following page and its root <item>.




Now let's get started with the coding.


First of all create the following model classes.


    public class FacebookRSS

    {

        public string Title { getset; }

        public string Link { getset; }

        public string Description { getset; }

        public string PubDate { getset; }

    }


    public class FBRssReader

    {

        public static IEnumerable<FacebookRSS> GetFeed()

        {

            var client = new WebClient();

            client.Headers.Add("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            var xmlData = client.DownloadString("https://www.facebook.com/feeds/notifications.php?id=164214527076029&viewer=108743653424029&key=ACk_XJqgt56etdrK&format=rss20");  //remember to update this URL


            XDocument xml = XDocument.Parse(xmlData);


            var FBUpdates = (from story in xml.Descendants("item")

                            select new FacebookRSS

                            {

                                Title = ((string)story.Element("title")),

                                Link = ((string)story.Element("link")),

                                Description = ((string)story.Element("description")),

                                PubDate = ((string)story.Element("pubDate"))

                            }).Take(10);


            return FBUpdates;

        }

    }


In the above code, we have two classes. The very first one "FacebookRSS" has a couple of properties and the second one "FBRssReader" has a method "GetFeed()" and this method will return IEnumerable data. The rest of the code is pretty simple and you can understand it easily. When you are done with the model, go ahead and create or update the existing controller.


        public ActionResult Index()

        {

            ViewBag.FBFeed = RSS_Reader.Models.FBRssReader.GetFeed();

            return View();

        }


Now, go ahead and update the view that will show our result on the web page.


<table>

<tr>

    <th>

        <h3>Facebook Updates</h3>

    </th>

</tr>


@foreach (var item in ViewBag.FBFeed)

{

<tr>

    <td>

        <b>@item.Title</b><a href="@item.Link" target="_blank"> more </a> on @Convert.ToDateTime(item.PubDate)

        <br />

        @Html.Raw(item.Description)

    </td>

</tr>

}

</table>


And now, I'm done with everything and am ready to run the application, here is my output screen:




Hope this helps. Thanks.

Login to add your contents and source code to this article
post comment
     

This looks interesting so I tried it. I am not failiar with MVC but I was able to stumble around in the dark and get this to work. Note that for finding the notifications URL it is not as direct as you show. What I did I think is, in my Facebook home page, click on the notifications icon (the globe?) and I think that took me to the page to see the RSS and then I got the URL. That was a little confusing for me since the key had a trailing underscore that did not get copied into my source code the first time so that did not work at first for me.

Posted by Sam Hobbs Dec 04, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter