user5950554
user5950554

Reputation:

How to get a list of fields from a sharepoint list with microsoft-graph?

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?

Upvotes: 0

Views: 1970

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

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();

Upvotes: 1

Related Questions