3

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 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Vadim. Can you check my updated question as I can't find a way to get a ListItemId?
Thanks again, Vadim. Your method does work! Is there some way to search/filter files only under a sharepoint folder (CustomerGroup/IndustryGroup)?
Instead of filtering, how would I simply return all custom columns and their values for each child item? By default they do not show, and ?expand=fields didn't work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.