Dave
Dave

Reputation: 1641

C# Facebook SDK and automatic paging

If I make a request for something that has a large number of objects (like if you had 10000 friends or 10000 photos in an album), does the C# Facebook SDK automatically follow the "Paging:Next" links for me, or is there anything I need to do?

I looked through their code and don't see any mention of paging, but could have missed it.

Note that I'm -not- talking about Batch requests; I'm speaking of a simple api.Get("/me/friends") where Facebook decides there are too many objects to put in a single response. Unfortunately I don't have an account with enough of anything to test the results...

Upvotes: 1

Views: 1656

Answers (3)

Dave
Dave

Reputation: 1641

Here's the code I wound up using. Since I know from the album's "count" how many images to expect I just request them in batches up to that count. It'd be trickier for scenarios where you don't know in advance how many objects you'll be getting back, but I haven't encountered a need for that yet.

const long maxBatchSize = 50;
for (long q = 0; q < album.Count; q += maxBatchSize)
{
    var facebook = new FacebookClient(FacebookSession.AccessToken);
    facebook.GetCompleted += new EventHandler<FacebookApiEventArgs>(GetPhotosCallback);

    long take = album.Count - q;
    if (take > maxBatchSize)
        take = maxBatchSize;

    dynamic parameters = new ExpandoObject();
    parameters.limit = take;
    parameters.offset = q;

    facebook.GetAsync("/" + album.Id + "/photos", parameters, null);
}

Upvotes: 1

Juha Palom&#228;ki
Juha Palom&#228;ki

Reputation: 27032

If you look at the data Facebook returns there is usually paging element within the results. The paging element contains next and previous urls. These URLs are created by Facebook and the "next" can be used to retrieve more information. Below is an example of the tokens for /me/posts -request:

{
 data: [ ... ],
 "paging": {
    "previous": "https://graph.facebook.com/6644332211/posts?limit=25&since=1374648795", 
    "next": "https://graph.facebook.com/6644332211/posts?limit=25&until=1374219441"
  }
 }

If you want to automated the retrieval of all items, you can pick the relevant parameters from the "next" url and pass them the Facebook SDK get method.

The relevant parameters you need to pick depend from the type of information you are retrieving. In posts you only have the "until", for checkins I can see also something called pagingtoken.

Upvotes: 0

DMCS
DMCS

Reputation: 31860

Pagination is always up to the user of the SDK, no matter which SDK for Facebook. I don't think they've gotten that creative in adding it in, or maybe there's some legal reasons they have not.

Upvotes: 1

Related Questions