Reputation:
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
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