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

Microsoft Security | Microsoft Graph
Developer technologies | C#
Developer technologies | 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.

Developer technologies | ASP.NET Core | Other

1 answer

Sort by: Most helpful
  1. Anonymous
    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

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.