0

I need to retrieve list items from a document library. The list items are retrieved by looking at a specific property named "X-transaction" (I have no control over the name of the property). I have a CSOM client that retrieves these items just fine using a CAML query, but we're migrating to Graph and I need to port the code over.

I have a properly configured GraphServiceClient that I have verified can be used to retrieve the title of the doc library. I'm trying to understand how to formulate the query. Should I be using Drives[drive.Id] to obtain the list items, or Lists[list.Id]?

When I try using drives:

var docLib = await ctx
    .Sites[site.Id]
    .Drives
    .Request()
    .GetAsync();
var drive = docLib.FirstOrDefault(d => d.Name == "DocLibraryName");

var filterQuery = $"field/X_x002d_transaction eq '{transactionId}'";
var items = await ctx.Drives[drive.Id]
            .Items
            .Request()
            .Filter(filterQuery)
            .GetAsync();

I get a syntax error exception:

Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: invalidRequest
Message: One of the provided arguments is not acceptable.
Inner error:
    Code: badArgument

I'm clearly misunderstanding the syntax here. How should this filter be formatted?

If I instead try to read from a list, I get a complaint that filters are not supported on lists. It appears that avenue is closed off: true or false?

1 Answer 1

0

I am assuming that you have correct graphclient object and permissions assigned to your app registrations with you. You can try below code chunk to filter the list/library:

var siteId = "your-site-id";
var driveId = "your-drive-id";
var fieldName = "X_x002d_transaction";
var fieldValue = "<filter-value>";

var queryOptions = new List<QueryOption>
{
    new QueryOption("$filter", $"{fieldName} eq '{fieldValue}'")
};

var items = await graphClient.Sites[siteId].Drives[driveId].Root.Children
        .Request(queryOptions)
        .GetAsync();

foreach (var item in items)
{
   Console.WriteLine(item.Name);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.