Reputation: 41
I'm trying to retrieve the items from a list in a SharePoint subsite with the .NET SDK. I can get the subsite, list, and items, but I don't know how to retrieve the columns. For example, here I'm trying to list all the titles of each list item:
IListItemsCollectionPage items = await graphClient
.Sites[targetstate_id]
.Lists[targetlist_id]
.Items
.Request()
.Select("Fields")
.GetAsync();
Console.WriteLine("Chosen ID: " + lists[listindex].Id);
Console.WriteLine("Number of items: " + items.Count);
for (var index = 0; index < items.Count; index++) {
//(attempting) at writing the title of each item in list
Console.WriteLine("Title " + items[index].Fields.AdditionalData["Title"]);
}
Console.WriteLine("Completed");
What's the issue here?
Upvotes: 2
Views: 1586
Reputation: 33122
The fields
property of a ListItem
is a collection that you need to "expand" rather than "select".
The code you're using here is executing the following query:
https://graph.microsoft.com/v1.0/sites/{id}/lists/{id}/items?$select=fields
What you want however is:
https://graph.microsoft.com/v1.0/sites/{id}/lists/{id}/items?$expand=fields
When using the SDK, you'll want something like this:
IListItemsCollectionPage items = await graphClient
.Sites[targetstate_id]
.Lists[targetlist_id]
.Items
.Request()
.Expand("fields")
.GetAsync();
Upvotes: 4