How good is your Memory? Part II

In my last post, I showed how easy it was to have the user log in to their Facebook account via Facebook Connect using the Facebook SDK for Silverlight and to then retrieve that user’s information. This post will take the working parts from Part I and expand on it by showing how to get the user’s list of friends and then grabbing the latest profile picture for those friends. I also hope to have a full working version of the game up by early next week that will be available on Facebook. I will post a link here when it’s available.

How Popular are You?

So in the last post, we got our logged in user, now let’s go find all their Facebook friends. To accomplish this, we will call the Facebook.Rest.Api.Friends.GetAsync method and we will pass in the uid of our user as well as a callback method that will handle the return from the api.

// here is the code for this method call_facebookApi.Friends.GetAsync((long)_currentUser.uid, GetFriendsCompleted, null);

Once we have the list of friends, we go out and use the Facebook.Rest.Api.Photos.GetAlbumsAsync method to retrieve all of the photo albums for each friend. For this demo, we will just grab the photo albums of the first twenty friends, in case they’re really popular. The GetAlbumsAsync methods takes in the uid for the friend we want to use and a callback method.

void GetFriendsCompleted(IList uids, object state, FacebookException e){    if (e == null)    {        Dispatcher.BeginInvoke(() =>        {            this.txtStatus.Text = "Loading Photos...";            // get the albums for our first 20 friends            for (int x = 0; x <; 20; x++)            {                _facebookApi.Photos.GetAlbumsAsync(uids[x], GetAlbumsCompleted, uids[x]);            }        });    }    else    {        Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting friends: " + e.Message));    }}

So now that we have our list of photo albums, we need to get the pictures in each album. With the albums retrieved, the first album in the list is usually the Profile Photos. To do this, we will use the Facebook.Rest.Api.Photos.GetAsyc method. We’ll pass in an empty string for the subj_id, album id at index 0, new string array for the list of photo ids, and a callback method.

void GetAlbumsCompleted(IList albums, object state, FacebookException e){    if (e == null)    {        Dispatcher.BeginInvoke(() =>        {            if (albums.Count > 0)            {                // get the photos from the first album, which is usually the profile pictures                _facebookApi.Photos.GetAsync(string.Empty, albums[0].aid, new List(), GetPhotosCompleted, state);            }        });    }    else    {        Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting albums: " + e.Message));    }}

Finally, this last callback will handle the photo’s that were returned from each query. The person’s most recent profile photo is located at index 0. To display these photos, we will create a new Image and then add it to a user control that was created to display the photos.

void GetPhotosCompleted(IList photos, object state, FacebookException e){    if (e == null)    {        Dispatcher.BeginInvoke(() =>        {            // create a new image            Image i = new Image();            i.Source = new BitmapImage(new Uri(photos[0].src));            i.Width = 140;            i.Height = 140;            i.Stretch = Stretch.Uniform;            i.Margin = new Thickness(0, 0, 5, 0);            // add the image to our user control            this.ucPhotoAlbum.AddPhoto(i);        });    }    else    {        Dispatcher.BeginInvoke(() => MessageBox.Show("Error: " + e.Message));    }}

Conclusion

So in this post we expanded on our last foray into the Facebook SDK and were able to retrieve the user’s list of friends and then get the latest profile picture for those friends. Hope you enjoyed this brief look at the Silverlight Facebook SDK.

Code!

Here is a download of a small demo application that uses the steps that were just discussed. In order to run the application, you will need to set up your own Facebook application. Until next time, Sláinte!

Download

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>