Forum Discussion

marwan2395's avatar
marwan2395
Copper Contributor
Feb 26, 2025

how to get files of a specific folder path in sharepoint using /lists graphapi

i was able to get folders of a specific folder path using /drives but in /lists it will return files also not just folders unlike /drives , i already made a python script to retrieve files from documents in sharepoint , but now i need to get files of a specific folder not the whole documents folders

here is the graph api url to retrieve folders from a specific path

https://graph.microsoft.com/v1.0/sites/siteId/drives/driveId/root:/General/index/images/animals:/children?$top=15

 

here is my python script to get files and folders using /lists

async def fetch_file_details(session, url, headers):

    async with session.get(url, headers=headers) as response:

        print(f"headers {response.headers} ")

        return await response.json()




async def get_all_images_in_library(accessToken, siteId, libraryId, batch_size=15):

    url = f"https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{libraryId}/items?top={batch_size}"

    headers = {

        'Authorization': f'Bearer {accessToken}',

        'Accept': 'application/json',

        'Prefer': 'HonorNonIndexedQueriesWarningMayFailRandomly'

    }




    async with aiohttp.ClientSession() as session:

        while url:

            async with session.get(url, headers=headers) as response:

                if response.status != 200:

                    print(f"Failed to fetch: {response.status}")

                    retry_after = response.headers.get('Retry-After')

                    throttle_limit_percentage = response.headers.get('x-ms-throttle-limit-percentage')

                    throttle_scope = response.headers.get('x-ms-throttle-scope')

                    throttle_reason = response.headers.get('x-ms-throttle-reason')

                    print(f"headers {response.headers} ")

                    if retry_after:

                        print(f"Retry-After: {retry_after} seconds")

                    if throttle_limit_percentage:

                        print(f"Throttle Limit Percentage: {throttle_limit_percentage}%")

                    if throttle_scope:

                        print(f"Throttle Scope: {throttle_scope}")

                    if throttle_reason:

                        print(f"Throttle Reason: {throttle_reason}")

                   

                    break

               

                data = await response.json()




            items = data.get('value', [])

            if not items:

                break




            tasks = []

            for item in items:

                webUrl = item.get('webUrl', '')




                if webUrl.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):

                    fileDetailsUrl = f"https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{libraryId}/items/{item['id']}?expand=fields"

                    task = fetch_file_details(session, fileDetailsUrl, headers)

                    tasks.append(task)




            if tasks:

                batch_results = await asyncio.gather(*tasks)

                yield batch_results




            await asyncio.sleep(0.1)

           

            url = data.get('@odata.nextLink')

 

my question is  how can i get files and folders from a specific path using /lists ?

Resources