Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
308 stars 42 forks source link

Downloading items by DocumentID #116

Closed KaiAragaki closed 1 year ago

KaiAragaki commented 2 years ago

Apologies if this isn't the correct place to ask this question, or if it has been answered previously - I checked but did not see any question that's asked this previously.

Is it possible to download an item from SharePoint/OneDrive using the stable DocumentID vs specifying a file path?

Thank you for producing an excellent package!

hongooi73 commented 2 years ago

No, there's currently no way to use the ID to download a file. Can I ask your use case?

KaiAragaki commented 2 years ago

I'm interested in using it as a more stable version of specifying a file path - as I understand, document IDs remain the same even if a file is moved or gets renamed

taylo5jm commented 1 year ago

Hi @hongooi73, I have a similar question.

We are looking to find and download files in 365 via metadata we've attached through Sharepoint. I believe the Microsoft term is "managed metadata".

I'm able to find files in this way by searching through Sharepoint lists, but since the ms_list_item class does not appear to have a download method, I'm thinking of using the webUrl attribute of the ms_list_item to essentially find the file by path in OneDrive where I can use the download method of the ms_drive_item class.

Is this the simplest way to approach the problem? If files could be downloaded by document ID, it seems like we could simply find the file in Sharepoint and download, rather than having to use both the Sharepoint and Onedrive facilities.

Here is an example of my current approach:

# Define an identifier to search by
external_id <- 000000000000
# Get the sharepoint site
mysite <- Microsoft365R::get_sharepoint_site(site_url="https://mycompany.sharepoint.com/sites/myteam")
# Get the sharepoint list
sharepoint_list <- mysite$get_lists()[[1]]

# Find the file in Sharepoint via external_id
query <- sharepoint_list$list_items(
    filter=glue::glue("startsWith(fields/ExternalID, {shQuote(external_id)})"))
# Get the ms_list_item
sharepoint_item <- sharepoint_list$get_item(query$id)

# Get the drive
sharepoint_drive <- mysite$list_drives()[[1]]
# Construct a path for `get_item`
item_path <- sharepoint_item$properties$webUrl %>% gsub(".+Shared%20Documents\\/(.+)", "\\1", .) %>%
    gsub("%20", " ", .)
# Get the drive item and download
drive_item <- sharepoint_drive$get_item(item_path)
drive_item$download()

Thanks for your work on this package!

hongooi73 commented 1 year ago

You can now access Onedrive/Sharepoint items by item ID instead of path, from the drive object:

od <- get_business_onedrive(...)
it <- od$get_item(itemid="xxxxxxx")
it$download(...)