0

I'm using MSGraphSDK to get all users using the Microsoft Graph - but I can only get the first batch of users (default batch size is 100). I am able to get the first batch as shown below, but I can't see how the framework supports getting the next batch...

func getUsers(...) {
  var i = 0

  self.graphClient.users().request().getWithCompletion{
    (collection:MSCollection?, nextLink:MSGraphUsersCollectionRequest?, error:Error?) in

    if let nsError = error {
      NSLog("failed - message: \(nsError.localizedDescription)")
    } else {
      if let users = collection {

        for user: MSGraphUser in users.value as! [MSGraphUser] {
          i = i+1
          print("\(i): \(user.optDisplayName ?? "<empty>")")

          self.save(user)
        }

        // TODO: Handle next batch...
        if users.nextLink != nil {
          //self.getNextUsers(users.nextLink)
        }
      }
    }
  }
}
David
  • 2,412
  • 1
  • 14
  • 22
Kim Rasmussen
  • 453
  • 1
  • 8
  • 21
  • Hi Kim, what values are you seeing for users.nextLink and nextLink? – David Jul 05 '17 at 23:52
  • I see the link for the next batch as expected... but the framework does not fetch them automatically and I can see how to use the nextlink in the request. – Kim Rasmussen Jul 06 '17 at 08:41

1 Answers1

0

users.nextLink is the URL link, whereas nextLink is a request object that has been initialized with the next page's URL that you can invoke with getWithCompletion and follows the same pattern as the request shown.

gkubed
  • 1,849
  • 3
  • 32
  • 45
dpim
  • 46
  • 3
  • Welll I'm new to Swift 3 (and iOS) - can you please provide an example... I only get an infinite loop... – Kim Rasmussen Jul 08 '17 at 09:19
  • In your implementation of getNextUsers - you should check to see if 'nextLink' has been set - if it is set, you have a new page available. Otherwise if it is nil, you're done. If you want to terminate early, I would add a parameter to the method keeping track of what the count of pages (or resources) has been up to a certain point so you can terminate when your limit has been reached. Does that help? – dpim Jul 10 '17 at 17:09
  • I can see how this is done using the SDK framework... You can add some options to the request() but nothing else. I tried to add the skiptoken, but it didn't work. I have actually skipped the MSGraphSDK and now use the p2.OAuth2. Now I can control the requests (needed a beta method and the delta request on the users which the SDK didn't support). But I think that the issue is still relevant for others using the MSGraphSDK... – Kim Rasmussen Jul 11 '17 at 07:36