Add Ons - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Add Ons arrow Page 4 - Using the Facebook API
Using the Facebook API
By Tim Stevens

Rate This Article: Add This Article To:

Using the Facebook API - ' Getting User Info '
( Page 4 of 4 )

Retrieving a User's Info

When retrieving info about a user, you don't always get everything that Facebook stores about the individual. In the Friends() call in the above example, the display name and a few other bits of data about friends are retrieved by default, but not much more. To get the rest, you need to rely on the DetailList class and the GetInfo function.

ADVERTISEMENT

DetailList exists to let you specify which bits of data you want returned from the Facebook services. By creating an instance of the class and calling the DetailList.Enable("attribute") method, you ensure that named attribute is returned. However, there are two points to keep in mind with the DetailList. First, you may find that you get more info than you specifically requested, which is not usually a problem. And second, it's worth noting that while the value for "attribute" typically matches one of the members of the Person class, the String to use to retrieve it corresponds to the Facebook API attribute name. So, to get the display name, pass in the string "name" as in the XML message, not "Name" as in the Person member. (Minor details.)

Now, to actually get the information, you pass the target UID, along with the DetailList, into the GetInfo function, which is contained in Api. The response is an instance of Person with all the appropriate information (and, perhaps, then some).

For the example application, that code is handled in the SelectedIndexChanged action. Whenever a friend is selected from the list, the mostly empty instance of Person is retrieved, the UID there is used to query the Facebook services, and the information, along with the user's picture, is displayed:

private void friendBox_SelectedIndexChanged(object sender, 
    EventArgs e)
{
    Person friend = (Person)friendBox.SelectedValue;
    DetailList deetList = new DetailList();
    deetList.EnableAll();

    if (friend != null)
    {
	friend = api.GetInfo(friend.Id, deetList);
	txtBirthday.Text = friend.Birthday.ToShortDateString();
	txtFirstName.Text = friend.FirstName;
	txtLastName.Text = friend.LastName;
	txtMusic.Text = friend.Music;
	homeTown = friend.Hometown;
	txtHomeTown.Text = friend.Hometown.City + ", " + 
        friend.Hometown.State + " " + friend.Hometown.Country;
	personPicture.LoadAsync(friend.Pic);
    }
}

In this case, the app is using DetailList.EnableAll() to ensure that all information about the user is retrieved. Most of those values are simply stored as Strings, but some — such as Hometown in this case (an instance of Location) — are structs. In this way, retrieving and processing user information is very straightforward.

Face First

The Facebook API provides a straightforward, though occasionally clumsy way to access information about your account, your friends, and even their pictures. Facebook itself is already a very full-featured site, especially compared to many other social networking portals on the Web, but there's still plenty of room for harvesting and displaying the limitless amount of data stored inside. With the info from the example above, you should now be able to do so.



 
 
>>> More Add Ons Articles          >>> More By Tim Stevens
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.