How to Download a Shared File on OneDrive for Business with the REST API?

2.7k Views Asked by At

I am trying to download a file from a shared folder that appears in the "Shared with me" folder of OneDrive for Business. I am using the following REST call to get the folder name and the files that appear in it:

https://{tenant}-my.sharepoint.com/_api/search/query?querytext='(SharedWithUsersOWSUSER:{UserAccountName} AND contentclass:STS_ListItem_MySiteDocumentLibrary)

and

https://{tenant}-my.sharepoint.com/_api/search/query?querytext='(ParentLink:{parentlink})'

with no issues. From the metadata supplied from these calls, I get the files in each directory and tried downloading them using the following GET request calls (note that "user1" is the person downloading the file, "user2" is the person that shared the file):

https://{tenant}-my.sharepoint.com/personal/user1_mydomain_com/_api/Web/GetFileByServerRelativeUrl('/personal/user2_mydomain_com/Documents/test/test1.docx')/$value

or

https://{tenant}-my.sharepoint.com/_api/v1.0/user2_mydomain_com/files/{fileid}

I have also attempted to use the new v2.0 calls, but they don't seem to support "Shared with me" folder/file access yet.

In all my attempts, I get a "Bad Request" or "Not found" error when trying to download a shared file. I have no issues downloading my own OneDrive for Business files using the following REST call:

https://{tenant}-my.sharepoint.com/personal/user_mydomain_com/_api/Web/GetFileByServerRelativeUrl('/personal/user_mydomain_com/Documents/User Folder/Test.htm')/$value

The calls I am using were obtained from:

List folder contents of 'Shared with Me' folder from Office 365 OneDrive (Sharepoint) for Business and Education using the REST api?

https://msdn.microsoft.com/office/office365/APi/files-rest-operations (v1.0 calls)

https://dev.onedrive.com/README.htm (v2.0 calls)

Any advice would be appreciated. Thanks!

0

There are 0 best solutions below