question

APIPointNewbie-3149 avatar image
0 Votes"
APIPointNewbie-3149 asked tinywang-msft commented

Search Files with Graph SDK

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

dotnet-csharpmicrosoft-graph-sdkdotnet-aspnet-core-mvcmicrosoft-graph-files
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @APIPointNewbie-3149,

Please read the SDK documentation for details on how to add the SDK to your project and create an authProvider instance.

0 Votes 0 ·

Hello,
this is what I have, I have coded a webhook with delta(Following these instructions) that works fine, only this search does not work properly.



0 Votes 0 ·

1 Answer

tinywang-msft avatar image
1 Vote"
tinywang-msft answered tinywang-msft commented

Hi @APIPointNewbie-3149 , 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



image.png (7.4 KiB)
image.png (74.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is strange: I didn't change anything in my original code and the search now works with my daemon service.

1 Vote 1 ·
tinywang-msft avatar image tinywang-msft APIPointNewbie-3149 ·

Hi @APIPointNewbie-3149 , it may take place when you newly set the api permission for you ad app and you authorize the client before the permission take effect. There's no error message for you issue so I'm afraid it hard to find out the reason why it's ok without any change on it. However, it worked any way, I think we can end this case : )

1 Vote 1 ·