0

Background

I have an azure function app that in part, includes the following logic:

       var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
        var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
        var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");

        string[] scopes = new[] { "https://graph.microsoft.com/.default" };
        
        // using Azure.Identity;
        var options = new ClientSecretCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        // using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential 
        var clientSecretCredential = new ClientSecretCredential(
            AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
        
        var result = await graphClient.Users["<myguid>"].GetAsync();
        return result;

Now I want to replace the last 2 lines with a call to Microsoft Search api. I've tested first via ms graph explorer like so:

enter image description here

It gives me the results I'm looking for. So I copied the c# code snippet from Graph Explorer into my project.

The code now looks like this:

       var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
        var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
        var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");

        string[] scopes = new[] { "https://graph.microsoft.com/.default" };
        
        // using Azure.Identity;
        var options = new ClientSecretCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        // using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential 
        var clientSecretCredential = new ClientSecretCredential(
            AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

       //****** THIS IS WHERE THE MS SEARCH API LOGIC STARTS ********//
       var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody
      {
         Requests = new List<SearchRequest>
        {
            new SearchRequest
                {
                EntityTypes = new List<EntityType?>
                {
                    EntityType.DriveItem,
                },
                Query = new SearchQuery
                {
                    QueryString = "some search criteria",
                },
                From = 0,
                Size = 35,
                QueryAlterationOptions = new SearchAlterationOptions
                {
                    EnableSuggestion = true,
                    EnableModification = true,
                },
            },
        },
     };
var result = await graphClient.Search.Query.PostAsync(requestBody);

But I'm getting some compile errors.

Versions

enter image description here

library references

enter image description here

Problems

The error I'm coming across is "The type or namespace name 'Beta' does not exist in the namespace 'Microsoft.Graph' (are you missing an assembly reference?)"

on this line:

 var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody

I added a baseUrl to the graphClient so it points to beta and then change the requestBody assignment.

 var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/beta/");

But it's not clear how I should change the call to Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody because I don't have that method in the GraphServiceClient I have created.

So I'm trying to do something like this:

enter image description here

The note in Graph Explorer indicates that the code snippets are all based on 5.x which I have. Any tips would be appreciated.

dot
  • 14,928
  • 41
  • 110
  • 218
  • Hi, when we use beta version graph API, we need url like `/beta/xxx` instead of `/v1.0/xxx`. And if we want to use beta version query via Graph SDK, we also need to use beta version. https://i.stack.imgur.com/jieVQ.png – Tiny Wang Jul 18 '23 at 05:58
  • 1
    by the way, if we installed beta version Microsoft.Graph, it might also require to use beta version of Azure.Identity.. not very sure about this point because I didn't test it recently. In the new version SDK, it might not demand it. Just a tip... – Tiny Wang Jul 18 '23 at 06:00
  • If you can use v1.0 then use `new Microsoft.Graph.Search.Query.QueryPostRequestBody` instead of `new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody`, otherwise you need to use nuget `Microsoft.Graph.Beta` – user2250152 Jul 18 '23 at 06:02
  • Please see update 1. I am no longer using beta. – dot Jul 18 '23 at 12:17
  • You have to look into the `ex.Error.InnerError` to get the concrete error message back. – Oliver Jul 18 '23 at 12:24
  • these are two completely separate issues, one is namespace, and another one is request exception. please close this one and post a new question including exception details – Yehor Androsov Jul 18 '23 at 12:24
  • @YehorAndrosov kk makes sense. Will close and post new – dot Jul 18 '23 at 12:34

1 Answers1

0

Per the suggested comments above, I changed the logic to not use beta:

  var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/v1.0/");
                                    
  var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody
dot
  • 14,928
  • 41
  • 110
  • 218