How to filter SharePoint listitems with PnP Core SDK

213 Views Asked by At

I'm unable to find a good example of reading sharepoint listitems and filtering on a custom column with the PNP.Core sdk library in C#.

I could filter on Title:

                var listItems = await pnpContext.Web.Lists.GetByTitle("TestList")
                    .Items
                    .Where(x => x.Title == "Foo" )
                    .ToListAsync();

But I would need something like this:

                var listItems = await pnpContext.Web.Lists.GetByTitle("TestList")
                    .Items
                    .Where(x => x["Status"] == "Completed" )
                    .ToListAsync();

The result api call should look like this (or the corresponding graph api call): _api/web/lists/getByTitle('TestList')/items/?$filter=Status eq 'Completed'

1

There are 1 best solutions below

1
ChengFeng - MSFT On BEST ANSWER

I checked the documentation, https://pnp.github.io/pnpcore/using-the-sdk/listitems-intro.html Maybe you can do this

var listItems = await context.Web.Lists.GetByTitle("TestList")
    .Items
    .Where(x => x.Values["Status"].ToString() == "Completed")
    .LoadAsync();

enter image description here