How to Access Shared Items in OneDrive for Business API?

I’m trying to get shared items from OneDrive for Business using their API. I can fetch my own files, but I can’t seem to find the ones shared with me. I’ve tried using GET /drive/root/children, but it doesn’t return the shared stuff.

Here’s what I know:

  • Shared folders should show up in the root
  • They’re supposed to have a ‘remoteItem’ facet

But I’m not seeing any of this. Is there a special way to get shared items? Maybe a different endpoint? I’ve looked through the docs and I’m stuck. Any help would be great!

# Example code (not working)
import requests

url = 'https://graph.microsoft.com/v1.0/me/drive/root/children'
headers = {'Authorization': 'Bearer ' + access_token}

response = requests.get(url, headers=headers)
items = response.json().get('value', [])

shared_items = [item for item in items if 'remoteItem' in item]
print(f'Shared items: {len(shared_items)}')  # Always 0

Am I missing something obvious here? Thanks in advance!

hey tom, i had same issue. try using /me/drive/sharedWithMe endpoint. it’ll give u all shared stuff. like this:

url = 'https://graph.microsoft.com/v1.0/me/drive/sharedWithMe'
response = requests.get(url, headers=headers)
shared_items = response.json().get('value', [])

works like charm for me. good luck!

I encountered a similar challenge when working with the OneDrive for Business API. The solution lies in using the correct endpoint. Instead of ‘/drive/root/children’, you should use ‘/me/drive/sharedWithMe’. This endpoint is specifically designed to retrieve items shared with you.

Here’s an adjusted version of your code that should work:

url = 'https://graph.microsoft.com/v1.0/me/drive/sharedWithMe'
headers = {'Authorization': 'Bearer ' + access_token}

response = requests.get(url, headers=headers)
shared_items = response.json().get('value', [])
print(f'Shared items: {len(shared_items)}')

This will return all items shared with you, including both files and folders. Remember to handle pagination if you’re dealing with a large number of shared items, as the API typically returns results in batches.

I’ve dealt with this issue before, and it can be frustrating. The key is to use the /me/drive/sharedWithMe endpoint instead of /drive/root/children. This endpoint specifically returns items that have been shared with you.

Here’s a modified version of your code that should work:

url = 'https://graph.microsoft.com/v1.0/me/drive/sharedWithMe'
headers = {'Authorization': 'Bearer ' + access_token}

response = requests.get(url, headers=headers)
shared_items = response.json().get('value', [])
print(f'Shared items: {len(shared_items)}')

This should return all items shared with you, including both files and folders. Keep in mind that the permissions on these items might vary, so you may need to check what actions you can perform on each item.

Also, if you’re dealing with a large number of shared items, you might want to implement paging to retrieve them all. The API typically returns results in batches, so you’ll need to use the ‘@odata.nextLink’ property to fetch subsequent pages.