Search code examples
javasharepointmicrosoft-graph-api

MS-Graph for Java 6.1, no children are listed


I'm using msgraph sdk for java (6.10). According to doc, when I want to list content of the folder in sharepoint, I can use something like this:

DriveItem root = graphClient.drives().byDriveId(driveId).root().get();
List<DriveItem> children = root.getChildren();

However, when I try to do that in my app, my children list is null. On the other hand, when I will do like this:

DriveItem root = graphClient.drives().byDriveId(driveId).root().get();
root.getFolder().getChildCount()

Child count will be appropriate (so it will show good amount).

So I'm not sure why I can't get driveitems in my first snippet?


Solution

  • You need to make an extra call to get children

    DriveItemCollectionResponse result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("root").children().get();
    

    By default only 200 items are returned. You need to use PageIterator

    DriveItemCollectionResponse result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("root").children().get();
    List<DriveItem> allDriveItems = new LinkedList<>();
    PageIterator<DriveItem, DriveItemCollectionResponse > pageIterator = new PageIterator.Builder<DriveItem, DriveItemCollectionResponse >()
            .client(graphClient)
            .collectionPage(result)            
            .collectionPageFactory(DriveItemCollectionResponse::createFromDiscriminatorValue)
            .processPageItemCallback(item -> {
                allDriveItems.add(item);
                return true;
            }).build();    
    pageIterator.iterate();