Get files from folder using pagination on graphapi

Andrada Bordei 0 Reputation points
2024-10-12T11:09:35.5866667+00:00

Hello,

I am trying using the Graph Api to return the items (files) from a specific folder using the following call:

https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items?$count=true&$filter=parentReference/id eq '<idoffolder>' and folder eq null

This call is returning all the files respecting the filtering conditions, in the used set I have more than 100 files.

However when I try to return files paginated applying lest say top 2

using the following API call:

https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items?$top=10&$count=true&$filter=parentReference/id eq '<idoffolder>' and folder eq null

the response does not contain any files, count is 0 and has a nextLink which again return 0. At the last call it returns the last 2 files from the folder.

Is there a way to return in a paginated manner data using the

https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items?$count=true&$filter=parentReference/id eq '<idoffolder>' and folder eq null api?

Thanks

Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
{count} votes

5 answers

Sort by: Most helpful
  1. Hitesh Pachipulusu - MSFT 3,620 Reputation points Microsoft External Staff
    2024-10-14T07:27:58.9233333+00:00

    Hello Andrada Bordei,

    Thank you for reaching out to Microsoft Support!

    It sounds like you're encountering an issue with pagination in the Microsoft Graph API when trying to retrieve files from a specific folder.

    When you use the $top parameter to limit the number of items returned, the API should paginate the results and provide a @odata.nextLink for the next set of items. However, it seems like the pagination isn't working as expected in your case.

    Use the Correct Endpoint: Make sure you're using the correct endpoint for listing items in a folder. The endpoint should be:

    GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/children?$top=2
    

    Please note $count is not supported when you use above Graph API.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    0 comments No comments

  2. Andrada Bordei 0 Reputation points
    2024-10-14T10:21:02.9733333+00:00

    Hello,

    Thank you for the answer. The issue is that I need the api to retrieve files from a specific folder based on the LastModifiedDateTime.

    If I use GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/children?$top=2

    I need also to apply $filter for the next situations:

    1. retrieve all the folders by setting $filter = folder ne null. This works as expected.
    2. retrieve all the files by setting $filter = folder eq null

    in https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items/{item-id}/children?$top=27&$filter=folder eq null

    this has as response:

    {

    *"error": {*
    
        *"code": "notSupported",*
    
        *"message": "The request is not supported by the system."*
    
    *}*
    

    }

    1. retrieve all the files modified after a particular date:

    https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items/{item-id}/children?$top=27&$filter=lastModifiedDateTime gt 2024-09-20T00:00:00Z

    the response is :{

    *"error": {*
    
        *"code": "invalidRequest",*
    
        *"message": "Invalid request"*
    
    *}*
    

    }

    After trying all the above I tried:

    "https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items?$top=250&$filter=parentReference/id eq '{item-id}' and folder eq null and lastModifiedDateTime gt 2024-10-06T13:31:27Z"

    However this call does not return all the items based on the top value. It returns 0 and provides the nextLink which again contains 0 and so one.

    Is there a way for this api to return the top as well?

    Thanks in advance.

    Andrada

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Andrada Bordei 0 Reputation points
    2024-10-14T11:01:01.55+00:00

    Hello,

    Based on empirical data, the SharePoint folder containing multiple files, folders and subfolders with files I observed that if the top value is higher than the count of all the folders and the files within the hierarchy of the folders then top does return partially data but not entirely (using the https://graph.microsoft.com/v1.0/sites/{{site_id}}/drives/{{drive_id}}/items?$top=250&$filter=parentReference/id eq '{item-id}' and folder eq null and lastModifiedDateTime gt 2024-10-06T13:31:27Z apic all) .

    Is there a way do determine how top is applied relatively to the folders structure and count of files?

    Thanks,

    Andrada

    0 comments No comments

  5. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2024-10-16T10:01:47.24+00:00

    Here are some explanations for the questions:

    1.We can get all folders through GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{item-id}/children?$filter = folder ne null

    2.Unfortunately, at the moment we can filter on folder property to get only folders. We cannot filter on folder or file property to get only files. $filter = folder eq null and $filter=file ne null are not supported currently

    3.To get all files exclude folder, you could use following Graph API.

    POST https://graph.microsoft.com/v1.0/search/query
    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "path:\"https://<tenant>.sharepoint.com/sites/<site_name>/<library_name>/<folder_name>\" AND isDocument=true"
                }
            }
        ]
    }
    

    4.When getting files from a folder, if the value of the top parameter is greater than the total number of subfolders and files in a folder, all subfolders and files will be returned in a page.

    For example:

    (1)There are 10 subfolders and files in a folder, you set $top=20, all subfolders and files returned in a page.

    (2)There are 10 subfolders and files in a folder, you set $top=5, 5 subfolders and files returned in a page, others in the next page.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.