Forum Discussion
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 ?
2 Replies
- grant_jenkinsIron Contributor
Not sure if this will get what you're after, but you could try the search endpoint.
https://graph.microsoft.com/v1.0/search/query
And in the Request Body
{ "requests": [ { "entityTypes": [ "listItem" ], "query": { "queryString": "path:https://YOUR_TENANT.sharepoint.com/sites/YOUR_SITE/LIBRARY/FOLDER1/FOLDER2" }, "fields": [ "id", "title", "createdBy", "createdDateTime", "lastModifiedBy", "lastModifiedDateTime", "webUrl", "contentType" ] } ] }One caveat though is that search will only return the last published version of a document depending on the library settings.
If you just wanted to return files (and not folders) you could add IsDocument:True to your query string.
"queryString": "path:https://YOUR_TENANT.sharepoint.com/sites/YOUR_SITE/LIBRARY/FOLDER1/FOLDER2 AND IsDocument:True"I'm not sure if we can filter with /list/GUID/items as you are after.
- marwan2395Copper Contributor
anyone ?