Search code examples
c#microsoft-graph-apionedrivemicrosoft-graph-sdks

File search with MS Graph SDK does not work


I have coded a webhook for One-Drive according to these instructions which also works but now I have the following problem:

I would like to search for a file with the Graph SDK(ASP.Net Core and C#), with Postman this also works as desired.

 https://graph.microsoft.com/v1.0/users/{user-id}/drive/root/search(q='Filename')

But I just can't get it to work with the SDK.

 private async Task<IDriveItemSearchCollectionPage> SearchFiles(GraphServiceClient graphClient){
   var search = await graphClient.Users["User-iD"].Drive.Root
     .Search("Filename")
     .Request()
     .GetAsync();
    
     return search;
 }

And the associated iterator:

 private void PrintFiles(IDriveItemSearchCollectionPage files) {

        foreach(var file in files) {
          var message = $"File: {file.Name}, Id: {file.Id}";

          Console.WriteLine(message);
        }
    }

What am I doing wrong ?


Solution

  • It's so crazy, I've been looking for a solution for days and nothing worked, and now it works even though I haven't changed anything in my code.