A few weeks back I wrote about the official C# SDK from Facebook and provided some sample code demonstrating how to use it to get some basic information about the logged in user. Admittedly the initial sample asp.net mvc code was fairly simple in order for it to be straight forward and easy to understand. I’ve decided to expand a bit on the sample in order to demonstrate how to use the Open graph API to perform a few more interesting calls, notably:
- Post to a user’s wall
- Retrieve a list of the user’s friends
- Retrieve a list of the user’s activities
The original sample code demonstrated how to authenticate a user and make calls using both the official C# sdk and a home grown solution. I’ve kept that approach but have organized and refactored the samples a bit better to make it hopefully more clear. The new version of the sample code allows you to login/out to facebook only on the home page. Once logged in you can go to the ‘Official’ and ‘Custom’ tabs to view your friends, activities or to post a message to your wall. While the functionality is the same between the two tabs, the code behind them is different. I assume most people would use the official SDK but I provided an alternative just as a point of interest. OK, I was bored really, and just wanted to see how it could be done differently.
In any case, the first step before running the code is to make sure you clear any previous permissions granted to this app (or whatever app you will be using with the sample code). The login button on the home page now requests extended permissions so you’ll probably need to remove it from your list of allowed applications if you’re running the latest code with a facebook app that was using the original sample code. The actual list of permissions being request are perms=’read_stream,publish_stream,read_friendlists,user_activities’, and the full list of permissions available can be found here http://developers.facebook.com/docs/authentication/permissions.
Official C# SDK
The code to use the Facebook API can be found in the OfficialFacebookController. By default index view will simply show the name of the logged in user, which uses the same code present in the HomeController (and the original post<)
string token = HttpUtility.UrlDecode(fbConnect.AccessToken); FacebookAPI api = new FacebookAPI(token); JSONObject me = api.Get(“/” + fbConnect.UserID); ViewData["Name"] = me.Dictionary["name"].String;

There are three other actions a logged in user can take here, view their friends list, view their activity list, and post a message to their wall.
-
Clicking the Get Friends button will call the GetFriends action in the controller. The call is simply api.Get("me/friends"), which returns a dictionary with a data key and an array of JSONObjects, representing all of the friends for the user. You could also call it with something like, api.Get("/" + fbConnect.UserID + "/friends"): "me" just uses the currently logged in user.
Facebook.FacebookAPI api = new Facebook.FacebookAPI(token); JSONObject me = api.Get("/" + fbConnect.UserID); JSONObject friendsData = api.Get("/me/friends"); var data = friendsData.Dictionary["data"]; List<JSONObject> friendsList = data.Array.ToList(); ViewData["Name"] = me.Dictionary["name"].String; ViewData["Friends"] = friendsList; The data itself is just the name and facebook id of the user's friends, so the view just loops through the friendsList collection and displays in this format Name - Id
<%= friend.Dictionary["name"].String %> - <%= friend.Dictionary["id"].String %>

-
Similarly, clicking the Get Activities button will call the GetActivities controller. The code is pretty much the same as the above friends code, but calls api.Get("/me/activities");. The data is also slightly different, since the activity data contains more information (such as category, and create_date). So, the view follows pretty much the same format but displays more fields:
<%= activity.Dictionary["name"].String%> - <%= activity.Dictionary["id"].String%>- <%= activity.Dictionary["category"].String%>- <%= activity.Dictionary["created_time"].String%>

-
Finally, the message section allows the user to enter a message and post it to a user's wall. Entering text and clicking the Post Message button will call the PostMessage action. Instead of calling the Get method of the FacebookAPI class, we call Post:
Dictionary
postArgs = new Dictionary (); postArgs["message"] = message; JSONObject post = api.Post("/" + fbConnect.UserID + "/feed", postArgs); 
The post call also returns a JSONObject which contains the id of the new post. Just for verification, we show this in the view.

Custom C# classes
The display of the custom code looks pretty much the same as the official sdk, so I won't post any images of the views. The primary classes involved are FacebookConnect.cs, FacebookObjects.cs and OpenGraph.cs. Note that compared to the original sample code I've removed the "DEMO" prefix and added a few more methods to OpenGraph, renamed the DEMOFacebookUser to FacebookObjects, and added objects to FacebookObjects.cs. These serve as the definition for our extra calls
You might also notice in FacebookObjects that the DataContract and DataMember attributes have been removed from FacebookUser. One of the more significant changes is the way in which the Call method in the OpenGraph class works. Initially it was using the DataContractJsonSerializer class, which required you to mark your class and it's properties with DataContract and DataMember attributes. However, this serializer seems to have some issues parsing certain JSON strings. So instead I've changed the code to use the JavascriptSerializer which seems to work just fine. In fact it's more flexible and seems to take less configuration than DataContractJsonSerializer, which is why it was probably undeprecated (dedeprecated?). In addition to the use of JavascriptSerializer, the Call method also takes an additional parameter to specify the type of http request made. This allows us to use the same code to make a POST request, which is necessary when writing to a user's wall. The new Call method looks like this:
private T Call<T>(string url, string methodType) where T : class
{
T result;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = methodType;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string jsonData = reader.ReadToEnd();
result = (T)jsSerializer.Deserialize<T>(jsonData);
}
return result;
}
The various Get/Post methods in the OpenGraph class are pretty similar to the original GetCurrentUser method and rely on the objects defined in the FacebookObjects.cs file. For example, the GetCurrentUserFriends looks like the following:
public List<FacebookFriend> GetCurrentUserFriends(string accessToken)
{
string parameters = "me/friends?access_token=" + accessToken;
string url = baseUrl + parameters;
FacebookFriendData call = Call<FacebookFriendData>(url, GET);
return call.Data;
}
The action just needs to call this method with a valid access_token. For example, the GetFriends action in CustomFacebookController, calls the following:
List<FacebookFriend> friends = openGraph.GetCurrentUserFriends(fbConnect.AccessToken);
Once this has been placed into the ViewData dictionary, we can just pull it out and iterate through it like so:
<%
List<FacebookSampleMVC2App.FacebookFriend> friendsList = ViewData["Friends"] as List<FacebookSampleMVC2App.FacebookFriend>;
foreach(FacebookSampleMVC2App.FacebookFriend friend in friendsList) {
%>
<%= friend.Name %> - <%= friend.Id %><br />
<% } %>
The logic/view for retrieving the user's activities and posting to the user's wall are fairly similar.
Wrap up
So there you have it, some sample code to do a bit more than just display the user's name and facebook id. The custom classes and the official sdk are pretty similar in the sense that they both just call REST methods like 'me/friends', 'me/activities' and 'me/feed' from https://graph.facebook.com/. The official SDK is more flexible however, since it doesn't need to have classes defined for each of the types, instead relying on JSONObject to wrap the dictionaries/arrays/scalars returned from the api calls.
Update - 11/15/2010
I recently updated the sample project with more samples, which can be viewed here: http://onishimura.com/2010/11/15/facebook-c-and-asp-net-mvc-updated-code-samples-for-places-wall-offline-access/

Hi!
Great post!!! I tried all the features introduced and everything works out of the box
Anyway, I’m having a really hard time to post messages to someone’s wall.
My code is as follows:
string token = “my token here”;
FacebookAPI api = new FacebookAPI(token);
Dictionary dic = new Dictionary();
dic["message"] = “Hello world”;
JSONObject post = api.Post(“/683334501/feed”, dic);
But I always get FacebookAPIException was unhandled, saying The remote server returned an error: (500) Internal Server Error.
Do you have any idea what might I be doing worng or how so solve this issue?
Many thanks in advance!
Best regards,
Ales
It could be the access_token you’re passing in, how are you obtaining it? The wrapper class I was using just pulls it out of the cookie, but you could also get it directly with something like this (which is commented out in HomeControler)
string token = HttpContext.Request.Cookies["fbs_" + ConfigurationManager.AppSettings["AppID"]]["\"access_token"];
fantastic.
the FIRST fully working example for MVC2 I find after looking for 10000 hours.
it would be great if you could add some more examples about this.
Thanks
actually I’m not getting any result when using the “get activities” button
Are you sure the account you’re getting the activites for actually has activities? When I first tried out the service I got nothing back, but that was only because I had nothing specified. You can add activites in facebook via the Info tab – Likes and Interests – Edit. Thats how I added the activities you see in the screenshot above (Dark Knight, Cross country running, etc), at least.
Thanks a lot it helped me a lot i was posting message using some other code which was giving some Server 400 exception. Thanks a lot once again
Thanks and How can I get my friend’s email?
I don’t think that’s possible actually….remember that the app has to get the user’s permission to even have access to their email (http://developers.facebook.com/docs/authentication/permissions). There’s really no way to get your friends email as well (e.g., the friends permission on email is unavailable). Makes sense really, getting all the friends emails could be easily abused by spammers, and likely cause an uproar over privacy concerns.
Hi!
Great post!
I tried to fix the problem that you wrote in the comments in this function
public bool IsConnected
{get
{
//Note that this can be insufficient in certain cases, such as when the user logs out of facebook in another window
//at that point the session/access_token is invalid
return (SessionKey != null && UserID > 0);
}}
but I didn’t succes.
I tried to put this function in the site .master after FB.init but it works only at login part, when the user login in another tab , it will also login on the site
FB.Event.subscribe(‘auth.sessionChange’, function (response) {
var hr = window.location.href;
if (response.session) {
// A user has logged in, and a new cookie has been saved
if (hr.indexOf(‘log=in’) = 0) { hr = hr + ‘&’; } else { hr = hr + ‘?’; } hr = hr + ‘log=in’; }
} else {
// The user has logged out, and the cookie has been cleared
if (hr.indexOf(‘log=in’) >= 0) {
hr = “?log=out”;
}
else {
if (hr.indexOf(‘log=out’) = 0) { hr = hr + ‘&’; } else { hr = hr + ‘?’; } hr = hr + ‘log=out’; }
}
}
window.location.href = hr;
});
can you please help me to solve this issue? thank you
hi, i am sorry if i am posting to wrong discussion, i have no understandding of coding, i found some codes to do application with iframe, everything works, it asks permission and etc..
i hope to add some kind of code, so everytime users uses my application there will be automatically some post added to his wall and etc. including a pic, link and some text.
is it possible? can you show me some ways please
thank you
i have tried the same thing in my code.
/me/feed api is posting to my facebook wall greatly but there’s one problem i have seen. when i am adding my dates into description feild of feed then it is adding exact 7 hours to the description date code.
please help.
Does anyone have any samples to pull logos off of Facebook pages?
How can I get my friend’s birthday?
Thanks for this wonderfull post, it helped me a lot.
thanks for the code
Hello can you invite friends without using the dialogs from facebook ? i mean with this c# sdk can you invite friends to your app ??
thnx. please any answers to dx1290@hotmail.com
Hello,
It is a good example for facebook application. I was using this example before and it was working fine. But since last week I observed that in FacebookConnedct.cs when I am calling IsConnected it is always returning false. I google and found that now facebook is using fbsr_ instead of fbs_ for its cookie name. I changed it but still GetFacebookCookieValue is returning null value. Please let me know how to solve this issue.
Thanks