Im trying to get the listItemId based on the Document-ID of a item in the SharePoint Library.
By Document-ID I mean the ID that is automatically assigned to a document when it is uploaded to sharePoint.
I have already successfully retrieved the driveItemId using the Document-ID. This is what the method looks like:
public async Task<string> GetDriveItemIdByDocIdAsync(string driveId, string docId)
{
try
{
var query = $"Dokument-ID:{docId}";
var results = await _graphClient.Drives[driveId].SearchWithQ(query).GetAsSearchWithQGetResponseAsync();
if (results.Value.Count == 1)
{
_logger.Info($"Found one document with documentId '{docId}' in drive '{driveId}'.");
return results.Value.FirstOrDefault().Id;
}
else if (results.Value.Count == 0)
{
_logger.Info($"No document with documentId '{docId}' found in drive '{driveId}'.");
throw new Exception($"No document with documentId '{docId}' found in drive '{driveId}'.");
}
else
{
_logger.Error($"More than one document with documentId '{docId}' found in drive '{driveId}'. Returning the first item with driveItemId == '{driveItemId}'");
return results.Value.FirstOrDefault().Id;
}
}
catch (ServiceException ex)
{
_logger.Error($"Error while trying to get last version of item with documentId '{docId}' in drive '{driveId}'. Exception: '{ex}'");
throw;
}
I need the listItemId to later read out the checkInComments of the different versions of the ListItem, as far as I know this is only possible via Lists, and not via Drives.
You do not need to answer directly with the C# SDK implementation, an https request is also sufficient. Thanks for your help
To get the SharePoint ListItemId, you can make use of below Microsoft Graph API query:
Posting details proof of work @user2250152
As you have DriveItemID you can pass it to get the list item ID
https://graph.microsoft.com/v1.0/drives/DriveID/items/DriveItemID/listItem
You can make use of select to fetch only the ID:
https://graph.microsoft.com/v1.0/drives/DriveID/items/DriveItemID/listItem$select=id
And use the below code:
public class GraphService
{
public GraphService(string clientId, string clientSecret, string tenantId)
{
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
_graphClient = new GraphServiceClient(credential);
}
private readonly GraphServiceClient _graphClient;
public async Task<string?> GetListItemIdAsync(string driveId, string driveItemId)
{
try
{
var listItem = await _graphClient.Drives[driveId].Items[driveItemId].ListItem.GetAsync();
return listItem?.Id;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error while trying to get list item with driveItemId '{driveItemId}' in drive '{driveId}'. Exception: '{ex}'");
throw;
}
}
}
public class Program
{
public static async Task Main(string[] args)
{
string clientId = "ClientID";
string clientSecret = "Secret";
string tenantId = "TenanTID";
var graphService = new GraphService(clientId, clientSecret, tenantId);
string driveId = "DriveID";
string driveItemId = "DriveItemID";
try
{
var listItemID = await graphService.GetListItemIdAsync(driveId, driveItemId);
Console.WriteLine($"List Item ID: {listItemID}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
The list item ID is fetched: