Search code examples
c#onedrivemicrosoft-graph-api

Microsoft Graph: How to receive list of remote items for drive?


How can I quickly receive list of drive items that are remote items (items that are links to items on other drives, shared with me items) ?

Of course I can to do something like it:

// Universal version
var res = new List<DriveItem>();
var list = await Client.Me.Drive.Root.Children.Request().GetAsync();
while (list != null)
{
    res.AddRange(list.CurrentPage.Where(i => (i.RemoteItem != null)));
    list = (list.NextPageRequest != null) ? (await list.NextPageRequest.GetAsync()) : null;
}
return res;

But it too slow, and requires parse all items. Can I use filter to resolve this task? In other words, will code below work on all types of drives (OneDrive Personal, Business, SharePoint ...)?

// Filtered version
var res = new List<DriveItem>();
var list = await Client.Me.Drive.Root.Children.Request().Filter("remoteItem ne null").GetAsync();
while (list != null)
{
    res.AddRange(list.CurrentPage);
    list = (list.NextPageRequest != null) ? (await list.NextPageRequest.GetAsync()) : null;
}
return res;

According to OneDrive documentation:

While the filter syntax can be used on any property of an item, we have optimized certain properties to be fast and efficient to filter on.

Note: In OneDrive for Business, SharePoint Online and SharePoint Server 2016, filtering support only name and url properties.

Does it mean that OneDrive for Business and SharePoint aren't support filtering by remoteItem facet?

Unfortunately Microsoft Grap documentation doesn't contain any additional information about it.

P.S.: I don't have OneDrive Business account, so, sorry can't check my assumption.


Solution

  • First things first, you should join the Office 365 Developer Program. This program includes a free Office 365 Developer license which will resolve your OneDrive for Business issue. There are several subtle differences between the two systems so testing against both is highly advised.

    As to your question, I would take a look at the sharedWithMe endpoint. This method returns a collection of items that have been shared with you from remote drives.

    I would also highly recommend pivoting to Microsoft Graph. It provides the same APIs as OneDrive but exposes a vast array of additional endpoints as well. A huge amount of effort is being funneled into Graph, making it a far better long-term strategy.