Facebook SDK C# - get friends list

23,324

Solution 1

Your function for storing friends list from json data:

string myAccessToken = "something";         
FacebookClient client = new FacebookClient(myAccessToken);         

var friendListData = client.Get("/me/friends");
JObject friendListJson = JObject.Parse(friendListData.ToString()); 

List<FbUser> fbUsers = new List<FbUser>();
foreach (var friend in friendListJson["data"].Children())             
{   
    FbUser fbUser = new FbUser();
    fbUser.Id = friend["id"].ToString().Replace("\"", "");                 
    fbUser.Name = friend["name"].ToString().Replace("\"", "");                 
    fbUsers.Add(fbUser);
}

Class for facebook user

Class FbUser {
    String Id { set; get; }
    String Name { set; get; }
}

Solution 2

As I had the same issue and was looking a nice solution. I did the same thing with linq resulting in less code. And most of the time less is more :)

FacebookClient client = new FacebookClient(accessToken);
dynamic friendListData = client.Get("/me/friends");
var result = (from i in (IEnumerable<dynamic>)friendListData.data
             select new
             {
                 i.name,
                 i.id
             }).ToList();
Share:
23,324
001
Author by

001

Only questions with complete answers are accepted as solutions.

Updated on September 13, 2020

Comments

  • 001
    001 over 3 years

    Using Facebook SDK (http://facebooksdk.codeplex.com/), you do something like this...

            string myAccessToken = "something";
            FacebookClient client = new FacebookClient(myAccessToken);
            IDictionary<string, object> friendData = (IDictionary<string, object>)client.Get("/me/friends");
    

    Now how do you get the friends data out from the dictionary?

  • achukrishnan
    achukrishnan almost 10 years
    how can we get birthday info along with that response ?any idea?. i used this but did't work ("/me/friends?fields=id,name,birthday");
  • Kiquenet
    Kiquenet over 9 years
    Not compiles for me in .net 4.5.1