0

How can I get the values of a ListItem from a SharePoint List via the .NET Graph SDK? I'm getting a "bad request" error.

var listItemData = graphClient
  .Sites["tenant.sharepoint.com:/sites/siteA:"]
  .Lists["List1"]
  .Items["117"]
  .Request()
  .Select("FullName,FirstName,Lastname")
  .GetAsync()
  .ResuIt;

When I use Graph Explorer I can fetch the fields but not from the SDK.

Is there a sample to get all ListItems and to print the field values to the console?

1 Answers1

1

The error occurs since the request returns ListItem resource:

var listItemData = graphClient.Sites["tenant.sharepoint.com:/sites/siteA:"]
.Lists["List1"].Items["117"].Request()
.Select("FullName,FirstName,Lastname").GetAsync().Result;
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                

but the properties specified in select expression belongs to ListItem.Fields property

To return ListItem field values, either replace

.Select("FullName,FirstName,Lastname")

with

.Expand("Fields($select=FullName,FirstName,Lastname)")

For example:

var request = await graphClient.Sites[{site-path}].Lists[{list-name}].Items[{item-id}].Request().Expand("Fields($select=FirstName,FullName)").GetAsync();

Or specify FieldValueSet resource endpoint, for example:

var request = await graphClient.Sites[{site-path}].Lists[{list-name}].Items[{item-id}].Fields.Request().Select("FirstName,FullName").GetAsync();
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193