How to filter SharePoint list item by ID using Microsoft Graph Api using C#?
1 Answer
If you are trying to fetch the SharePoint list item using item Id, you can use HTTP API like:
https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}?expand=fields
Using C#:
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items["{listItem-id}"].GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Expand = new string []{ "fields" };
});
Source Documentation:
-
Thank you much for the reply. Issue using this is, if a {listItem-id} not exists, .Items["{listItem-id}"] throws error.user109360– user1093602024-01-14 10:21:18 +00:00Commented Jan 14, 2024 at 10:21
-
You're welcome. If you are trying to get the list item by using ID, there should be an item with ID in the list. If you are using a variable for storing item ID and passing it to the API, you can first check if the variable is empty or not and call API conditionally OR if variable is having valid number but the item is not present in the list (maybe item deleted after creation or you are using wrong ID), you should use try-catch and exception handling in such cases.Ganesh Sanap - MVP– Ganesh Sanap - MVP2024-01-14 16:48:24 +00:00Commented Jan 14, 2024 at 16:48
-
Thank you! Can we have a filter query using listItem-id so that an error can be avoided, if item not exists.user109360– user1093602024-01-14 17:17:49 +00:00Commented Jan 14, 2024 at 17:17
-
Try something like this:
https://graph.microsoft.com/v1.0/sites/16f13df6-5f4k-4168-8165-ddc5ca261dd0/lists/5d9b63cd-5630-476d-b5fb-1641f07a630d/items?expand=fields(select=id,Title)&filter=(fields/id eq '2'). Check this and this.Ganesh Sanap - MVP– Ganesh Sanap - MVP2024-01-15 05:18:03 +00:00Commented Jan 15, 2024 at 5:18 -
Thank you! Using id in filter query, its getting an error 'A provided field name is not recognized'.user109360– user1093602024-01-15 13:35:05 +00:00Commented Jan 15, 2024 at 13:35