Search Files with Graph SDK

APIPointNewbie 146 Reputation points
2021-08-13T18:50:35.887+00:00

Hello, 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 (empty collection).

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 ?

I would be very happy to receive help.

Greetings

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,199 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,679 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,291 questions
{count} votes

Accepted answer
  1. Tiny Wang-MSFT 1,571 Reputation points Microsoft Vendor
    2021-08-17T09:25:59.923+00:00

    Hi @APIPointNewbie , this is my code and my test result. Pls note that you need to give the api permission in azure portal before, here I used client credential flow, so I set application permission but not delegate permission.

    using Azure.Identity;  
    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.Graph;  
    using System.Linq;  
    using System.Threading.Tasks;  
      
    namespace WebApplication3.Controllers  
    {  
        public class HomeController : Controller  
        {  
            public IActionResult Index()  
            {  
                return View();  
            }  
      
            public async Task<string> testAsync() {  
                var def = new[] { "https://graph.microsoft.com/.default" };  
                var tenantId = "tenant_name.onmicrosoft.com";  
                var clientId = "azure_ad_app_id_here";  
                var clientSecret = "client_secret_here";  
      
                var options = new TokenCredentialOptions  
                {  
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud  
                };  
                var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);  
                var graphClient = new GraphServiceClient(clientSecretCredential, def);  
                DriveItemSearchCollectionPage a = (DriveItemSearchCollectionPage)await graphClient.Users["my_user_id"].Drive.Root.Search("win-arm").Request().GetAsync();  
                return a.FirstOrDefault<DriveItem>().Name;  
            }  
        }  
    }  
    

    123941-image.png
    123850-image.png


    If the answer is helpful, please click "Accept Answer" and upvote it.
    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.
    Best Regards,
    TinyWang

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful