Comments (5)
Since there is some confusion around how to page through collections in this SDK, I added a wiki page devoted to this instead of cramming it into the examples page. Do you think this helps?
from msgraph-sdk-php.
Have you tried using a CollectionRequest instead of a regular request object?
$userIterator = $graph->createCollectionRequest("GET", "/users")
->setReturnType(Model\User::class)
->setPageSize(100);
$users = $userIterator->getPage();
foreach ($users as $user){
$userArray[] = $user->getDisplayName();
}
from msgraph-sdk-php.
@Cbales thank you for your quick answer, I really appreciate it.
I have tried CollectionRequest as you proposed and it seems like I'm getting a proper results now. (It is a very large collection so I'll have to examine its contents some more to make sure that all pages are properly included).
Related to your code sample, I changed it a bit to be able to create an unique array with all the results. After reading trough your class I found isEnd() function and combined it with getPage() to iterate trough everything.
Right now, that part of the code looks like this:
$userIterator = $graph->createCollectionRequest("GET", "/users")
->setReturnType(Model\User::class)
->setPageSize(100);
while (!$userIterator->isEnd()) {
$users = array_merge($users,$userIterator->getPage());
}
Maybe you want to add this part with while() iterator to your documentation as well, just to make it more clear that whole array (with all of the results) can be created easily.
@Daveismyname thanks for the idea! Yes, this seems like a very nice approach to handle pagination issue using just Guzzle. Since my work is not related to Microsoft API only, and I do plan to use Guzzle quite a bit, I will use this/your approach in my pure-Guzzle parts of the code. I especially like the part with Api class for the re-usability.
So, thank you both, I believe that this tread will be helpful to the other users of SDK as well, we can mark this issue as resolved now. :) If I have any more useful materials related to this subject I will add it in future.
from msgraph-sdk-php.
@milanlatinovic I went the guzzle route mainly due to not getting a response to my issue. I have today but I've already moved away from the SDK so how I'm getting the records is like this:
This is taken from a Laravel project (I've released a Laravel package for this https://github.com/daveismynamelaravel/MsGraph)
public function index()
{
$limit = 25;
$offset = 50;
$skip = request('next', 0);
$messageQueryParams = array (
//"\$select" => "subject,receivedDateTime,from",
"\$orderby" => "displayName",
"\$skip" => $skip,
"\$top" => $limit,
"\$count" => "true",
);
$contacts = Api::get('me/contacts?'.http_build_query($messageQueryParams));
$previous = null;
$next = null;
$total = $contacts['@odata.count'];
if (isset($contacts['@odata.nextLink'])) {
$first = explode('$skip=', $contacts['@odata.nextLink']);
$skip = explode('&', $first[1]);
$previous = $skip[0]-$offset;
$next = $skip[0];
if ($previous < 0) {
$previous = 0;
}
if ($next == $total) {
$next = null;
}
}
return view('office365::admin.contacts.index', compact('contacts', 'previous', 'next'));
}
The Api::get is calling a class where I have guzzle setup for reusability:
class Api
{
protected static $baseUrl = 'https://graph.microsoft.com/beta/';
public static function __callstatic($function, $args) {
$options = ['get', 'post', 'patch', 'delete'];
$path = (isset($args[0])) ? $args[0] : null;
$data = (isset($args[1])) ? $args[1] : null;
if (in_array($function, $options)) {
return self::guzzle($function, $path, $data);
}
}
protected static function guzzle($type, $request, $data = []) {
$client = new \GuzzleHttp\Client;
$tokenCache = new Token();
$response = $client->$type(self::$baseUrl.$request, [
'headers' => [
'Authorization' => $tokenCache->getAccessToken(),
'content-type' => 'application/json',
],
'body' => json_encode($data),
]);
return json_decode($response->getBody()->getContents(), true);
}
You might not want to do this way and stick with the SDK but it's an option.
from msgraph-sdk-php.
@milanlatinovic Thank you for your code example.
I can only use
while (!$userIterator->isEnd()) { ... }
when I do not use ->setPageSize(100)
in the query. When I use ->setPageSize(100)
, I only get the first page.
Thanks again for sharing!
from msgraph-sdk-php.
Related Issues (20)
- Migrating from 1.96 to 2.4 booking service issue HOT 1
- V2.4: getMessages and getChildFolders are empty HOT 2
- 404 on large file upload for todo task attachment
- branching normalization HOT 1
- Uncaught Error: Class "Microsoft\Graph\Test\AttachmentItem" HOT 2
- $graphClient->me()->drive() get invalid garant HOT 1
- public folders / calendars HOT 3
- add release please configuration
- Class 'Microsoft\Graph\Graph' not found (Nette 3x) HOT 2
- GraphRequestAdapter must be a subclass of SerializationWriterFactory::class HOT 9
- Question regarding 1.x usage HOT 2
- Files upload to the OneDrive though Laravel without user login HOT 2
- Fatal error: Uncaught Error: Class "Microsoft\Graph\Generated\BaseGraphClient" not found in vendor\microsoft\microsoft-graph\src\GraphServiceClient.php HOT 2
- Unable to recognize the Microsoft/Graph/Graph import HOT 1
- Undifined classes HOT 1
- Conflict Behavior cannot be set for Drive Item Copy HOT 1
- Serialisation/deserialisation no longer works HOT 2
- How to Retrieve MS Graph User email and pass extra parameters to callback redirect URL? (Auth, Login with Microsoft button)
- Logging example would be useful
- Only NULL on getMember from groups. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgraph-sdk-php.