Python Forum
Getting a DriveItem object from msgraph using relative path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a DriveItem object from msgraph using relative path
#1
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 drive/items/{item-id}
* By file system path using /drive/root:/path/to/file

I'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.xlsx
So 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.xlsx

I 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 Huh
Reply
#2
Perhaps this can help someone in the future...

The URL should be
api_base_url = "https://graph.microsoft.com/v1.0" site_domain = "myCompany.sharepoint.com" from urllib.parse import quote raw_url = f'{api_base_url}/{site_domain}/drives/{drive.id}/root:/{quote(web_url)}:/content'
So drives/root should've been drives/drive_id/root (doesn't feel very well documented)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  import a function from another file using relative path paul18fr 6 9,674 Aug-01-2024, 06:40 AM
Last Post: paul18fr
Smile Python & MSGraph - Learning to Walk ITMan020324 2 1,630 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 4,061 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  How to reference the relative directory when creating a photoimage kenwatts275 3 10,942 May-18-2021, 07:22 PM
Last Post: menator01
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 10,602 Oct-05-2019, 06:20 PM
Last Post: Bmart6969
  function wanted: resolve relative path Skaperen 4 5,216 Sep-06-2018, 01:52 AM
Last Post: Skaperen
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 9,930 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  Python 3.6.5 pathlib weird behaviour when resolve a relative path on root (macOs) QbLearningPython 7 9,974 May-29-2018, 08:38 AM
Last Post: QbLearningPython
  getting a full path string from a pathlib.PurePath object Skaperen 14 162,421 Mar-24-2018, 03:55 AM
Last Post: Skaperen
  ValueError: Attempted relative import in non-package JoeB 1 14,616 Mar-08-2018, 11:01 AM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.