My goal is to read out the driveItemId from the Document-ID. “Dokument-ID” is a unique SharePoint collumn that is given to every file in the SharePoint Library.
I tried using this method below to get a driveItem (its driveItemId) using Search with a query.
public async Task<string> GetDriveItemIdAsync(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
{
var driveItemId = results.Value.FirstOrDefault().Id;
var message = "Dokumente: n";
foreach (var item in results.Value)
{
message += $"Name: {item.Name}, Id: {item.Id}n";
}
_logger.Error($"More than one document with documentId '{docId}' found in drive '{driveId}'. Returning the first item with driveItemId == '{driveItemId}' n{message}");
MessageBox.Show($"Mehr als ein Dokument mit der DocumentId '{docId}' in Laufwerk '{driveId}' gefunden. Melden Sie sich sofort bei der IT und zeigen Sie diese Fehlermeldung. n{message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
return driveItemId;
}
}
catch (ServiceException ex)
{
_logger.Error($"Error while trying to get last version of item with documentId '{docId}' in drive '{driveId}'. Exception: '{ex}'");
throw;
}
}
I haven’t quite understood query yet, with certain document-ID inputs I get no item back at all, and with others I get much more than just one item back, even though the document ID column doesn’t match at all.
It is behaving very strangely, probably because my query is not yet correct.
Thanks for your help, im also open for a better solution for getting the driveItemId by Document-ID in a SharePoint Library.