Aug-07-2025, 07:08 PM (This post was last modified: Aug-07-2025, 07:51 PM by mikisDeWitte.)
I'm trying to download a file from sharepoint using the microsoft SDK for python msgraph-sdk (https://github.com/microsoftgraph/msgrap.../tree/main).
According to the documentation, there are two primary ways of addressing a driveItem resource:
* By the driveItem unique identifier using
* By file system path using
I'm using the 2nd approach (which points here: https://learn.microsoft.com/en-us/graph/...driveitems)
The webUrl to my file =
So I would expect that my graph url would be
I originally went ahead with the approach here:
https://stackoverflow.com/questions/7896...m-onedrive
I guess I could start writing a recursive function to follow the crumbs, but that feels like a hack for a super common feature?
This is essentially my code:
Anybody having any clue as to how I can easily turn my webUrl into a DriveItem object
According to the documentation, there are two primary ways of addressing a driveItem resource:
* By the driveItem unique identifier using
drive/items/{item-id}* By file system path using
/drive/root:/path/to/fileI'm using the 2nd approach (which points here: https://learn.microsoft.com/en-us/graph/...driveitems)
The webUrl to my file =
https://myCompany.sharepoint.com/:x:/r/sites/MySite/Shared%20Documents/Relative/Path/To/file.xlsxSo I would expect that my graph url would be
https://graph.microsoft.com/v1.0/sites/myCompany.sharepoint.com:/drives/{id of Shared%20Documents}/root:/Relative/Path/To/file.xlsxI originally went ahead with the approach here:
https://stackoverflow.com/questions/7896...m-onedrive
childItems = await self.client.drives.by_drive_id(drive_id) .items.by_drive_item_id('root').children.get()But this only gets the folders directly under my Shared Documents folder (in my case, the Relative folder)I guess I could start writing a recursive function to follow the crumbs, but that feels like a hack for a super common feature?
This is essentially my code:
from msgraph import GraphServiceClient from azure.identity.aio import ClientSecretCredential import asyncio site_domain = "myCompany.sharepoint.com" api_base_url = "https://graph.microsoft.com/v1.0" # get the site id async def get_site_id_by_name(client: GraphServiceClient, site_name: str) -> str: # this works nicely raw_url = f'{api_base_url}/{site_domain}:/sites/{site_name}' site_with_raw_url = await .client.sites.with_url(raw_url=raw_url).get() return site_with_raw_url.additional_data.get('id', '') async def get_file_from_web_url(client: GraphServiceClient, site_id: str, web_url: str) -> Optional[DriveItem]: query_params = GetAllSitesRequestBuilder.GetAllSitesRequestBuilderGetQueryParameters( filter=f"webUrl eq '{web_url}'", ) request_configuration = RequestConfiguration(query_parameters=query_params) drive = await client.sites.by_site_id(site_id).drive.get(request_configuration=request_configuration) #What I thought would work, but doesn't: from urllib.parse import quote # https://graph.microsoft.com/v1.0/sites/myCompany.sharepoint.com:/drive/[driveID]:/Relative/Path/To/file.xlsx raw_url = f'{self.api_base_url}/{self.site_domain}:/drives/{drive.id}/root:/{quote(web_url)}' result = await ( client .drives.by_drive_id(drive.id) .with_url(raw_url=web_url) .get() ) async def execute_functions(client: GraphServiceClient): site_id = await get_site_id_by_name(client, "MySite") file = await get_file_from_web_url(site_id, "Relative/Path/To/file.xlsx") credentials = ClientSecretCredential( tenant_id='tenant_id', client_id='client_id', client_secret='client_secret' ) client = GraphServiceClient( credentials=credentials, scopes=['https://graph.microsoft.com/.default'] ) asyncio.run(execute_functions(client))But I keep getting error: MainError(..., message='Url specified is invalid.', target=None)Anybody having any clue as to how I can easily turn my webUrl into a DriveItem object
