3
votes

In SharePoint Online site via my Office 365 account, I've added a column - "CustomerId" to my documents. I want to find all documents with CustomerId of 102 in C# (not in JavaScript).

So far, I'm able to get all files under a folder

var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
            .Children.Request().GetAsync().Result;

Or see the same result in Graph Explorer https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/children

but I haven't figured out the correct syntax to get all documents (driveIems) using the custom column filter condition in C# or in Graph Explorer. Examples of things I've tried:

In C#

var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
            .Search("fields/CustomerId eq 102").Request().GetAsync().Result;

In Graph Explorer https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/search(q='CustomerId eq 102')

Hope someone can help me out on this.

Update:
Previously I got the driveItemId from

var customerFolder = graphClient.Sites[siteId].Drive.Root
    .ItemWithPath("CustomerGroup/IndustryGroup").Request().GetAsync().Result;
string driveItemId = customerFolder.Id;

I see I can get a ListItem

var customerFolder = graphClient.Sites[siteId].Drive.Root
    .ItemWithPath("CustomerGroup/IndustryGroup").Request()
    .Expand(d => d.ListItem).GetAsync().Result;

but I only found a list ID of "4" from customerFolder.ListItem.Id

How shall I get a list ID so that I can use it in graphClient.Sites[siteId].Lists[listId]?

1

1 Answers

4
votes

I would suggest to utilize the following query:

https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?filter=fields/CustomerId eq 123&expand=driveItem

Explanation:

  • filter items in a list via filter query option
  • return associated drive items for a list item via expand query option

Here is an example for msgraph-sdk-dotnet:

var request = await graphClient.Sites[siteId].Lists[listId].Items.Request().Filter("fields/CustomerId eq 123").Expand(item => item.DriveItem).GetAsync();
foreach (var item in request)
{
    Console.WriteLine(item.DriveItem.WebUrl);
}

Update

The underlying document library list (along with its properties) for a drive could be retrieved like this:

var list = await graphClient.Sites[siteId].Drive.List.Request().GetAsync();
Console.WriteLine(list.Id);  //gives SharePoint List Id

Note: https://graph.microsoft.com/beta/sites/{site-id}/drive endpoint returns the default drive (document library) for this site

Reference

Working with SharePoint sites in Microsoft Graph