Hi @Daniel Mande ,Welcome to Microsoft Q&A,
In the .NET SDK for the Microsoft Graph API, the SearchWithQ
method is used to search for files or folders in a OneDrive or SharePoint drive.
// File name for search
var fileName = "file name or part of the name";
try
{
// Call SearchWithQ method to search for files
var searchResults = await GraphClient
.Drives[_Drive.Id] // Target drive ID
.Root // Search in the root directory (or specify a subdirectory)
.SearchWithQ(fileName)
.Request()
.GetAsync();
// Traverse search results
foreach (var item in searchResults)
{
Console.WriteLine($"Found file: {item.Name}, file ID: {item.Id}");
}
}
catch (ServiceException ex)
{
// Error handling
Console.WriteLine($"Search failed: {ex.Message}");
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.