GraphServiceClient - how to define the request body?

496 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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