Facebook Getting a list of albums

Getting a list of albums

If you're using v2.4 or newer of the Facebook Graph API, the example below will not work due to breaking changes introduced by Facebook.

For the call to work, you should at least specify the type field. You can read more about fields further down this page.

The GetAlbums method lets you request a list of albums of a user, page or similar. The method has a few overloads, and in it's simplest form you can specify the identifier you wish to get albums of.

The method will return an instance of FacebookAlbumsResponse, and each album can be accessed through Body.Data as shown in the example below:

@using Skybrud.Social.Facebook.Models.Albums
@using Skybrud.Social.Facebook.Responses.Albums
@inherits System.Web.Mvc.WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{
    
    // Make the request to the API ("me" refers to albums of the authenticated user)
    FacebookAlbumsResponse response = Model.Albums.GetAlbums("me");

    // Loop through the albums
    foreach (FacebookAlbum album in response.Body.Data) {

        // Write some of the album information
        <p>@album.Id: @album.Name</p>

    }
    
}

Facebook will by default return a list of the 25 most recent albums. To request more (or less) albums for each call, you can specify a limit:

@using Skybrud.Social.Facebook.Models.Albums
@using Skybrud.Social.Facebook.Responses.Albums
@inherits System.Web.Mvc.WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{
    
    // Make the request to the API ("me" refers to albums of the authenticated user)
    FacebookAlbumsResponse response = Model.Albums.GetAlbums("me", 50);

    // Loop through the albums
    foreach (FacebookAlbum album in response.Body.Data) {

        // Write some of the album information
        <p>@album.Id: @album.Name</p>

    }
    
}

The maximum limit is currently 50. If a higher limit is specified, the API will simply return 50 albums.

Pagination

Getting other pages than just the first one becomes a little more advanced. When getting a list of albums, Facebook specifies cursors/tokens for getting the next or previous page. Advanced options like paging cursors are specified by passing an instance of the FacebookGetAlbumsOptions class as the first parameter:

@using Skybrud.Social.Facebook.Models.Albums
@using Skybrud.Social.Facebook.Options.Albums
@using Skybrud.Social.Facebook.Responses.Albums
@inherits System.Web.Mvc.WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // Initialize the options for the call
    FacebookGetAlbumsOptions options = new FacebookGetAlbumsOptions {
        Identifier = "me"
    };
    
    // Get the first page
    FacebookAlbumsResponse response = Model.Albums.GetAlbums(options);

    // Loop through the albums
    foreach (FacebookAlbum album in response.Body.Data) {

        // Write some of the album information
        <p>@album.Id: @album.Name</p>

    }
    
    // Check whether there is a second page
    if (!String.IsNullOrWhiteSpace(response.Body.Paging.Next)) {

        // Update the options with the "after" cursor
        options.After = response.Body.Paging.Cursors.After;

        // Get the second page
        response = Model.Albums.GetAlbums(options);

        // Loop through the albums
        foreach (FacebookAlbum album in response.Body.Data) {

            // Write some of the album information
            <p>@album.Id: @album.Name</p>

        }
        
    }
    
}

While Next and After are used for getting the next page (back in time), you can also use Previous and Before to go forward in time (at least when not on the first page).

Fields

The Facebook Graph API will only return a partial object for each album by default - that is a JSON object containing the created_album, id and name fields. If you need other fields, you will have to specify the explicitly. This can be done as below:

@using Skybrud.Social.Facebook.Models.Albums
@using Skybrud.Social.Facebook.Options.Albums
@using Skybrud.Social.Facebook.Responses.Albums
@inherits System.Web.Mvc.WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // Initialize the options for the call
    FacebookGetAlbumsOptions options = new FacebookGetAlbumsOptions {
        Identifier = "me",
        Fields = "id,name,created_time,description,type"
    };

    // Make the request to the API
    FacebookAlbumsResponse response = Model.Albums.GetAlbums(options);

    // Loop through the albums
    foreach (FacebookAlbum album in response.Body.Data) {

        // Write some of the album information
        <p>
            @album.Id: @album.Name<br />
            <span style="font-size: 12px;">@album.Description</span>
        </p>

    }
    
}

You can find an updated list of available fields for an album in the Facebook Graph API reference. You can also read more about Facebook fields in Skybrud.Social.