How to use Graph API Sharepoint Search from console app

Christoph Piock-Ellena 1 Reputation point
2022-10-26T11:18:08.85+00:00

I have created a .net core console application to access the graph api. I created a authentication by using clientId and clientSecret of the Azure AD application
string tenantName = "MY.TENANT";
string authUrl = "https://login.microsoftonline.com/" + tenantName;
var clientId = "MYID";
var clientSecret = "MYSECRET";
AuthenticationContext authenticationContext = new AuthenticationContext(authUrl, false);

ClientCredential clientCred = new ClientCredential(clientId, clientSecret);  
AuthenticationResult authenticationResult;  

authenticationResult = await authenticationContext.AcquireTokenAsync("https://graph.microsoft.com/", clientCred);  
return authenticationResult.AccessToken;  

After I get a valid token the call do a sharepoint list works fine and I get some data

using var client = new HttpClient();  
var request = new HttpRequestMessage(HttpMethod.Get, $"{graphUrl}/sites/{siteId}/lists/MYLISTGUID/items?expand=fields");  
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);  
  
        var response = await client.SendAsync(request);  
        if (response.IsSuccessStatusCode)  
        {  
            var responseString = response.Content.ReadAsStringAsync().Result;  
            return responseString;  
        }  

But if I call the Search API I get the following error: SearchRequest Invalid (Region is required when request with application permission.)

using var client = new HttpClient();  
            var request = new HttpRequestMessage(HttpMethod.Post, $"{graphUrl}/search/query/");  
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);  
  
            var filter = new  
            {  
                Requests = new[] {   
                    new {  
                        EntityTypes = new[] { "listItem" },  
                        Query = new  
                        {  
                            QueryString = "Pio*"  
                        }  
                    }  
                }  
            };  
  
            request.Content = new StringContent(JsonConvert.SerializeObject(filter), Encoding.UTF8, "application/json");  
  
            var response = await client.SendAsync(request);  
            if (response.IsSuccessStatusCode)  
            {  
                var responseString = response.Content.ReadAsStringAsync().Result;  
            }  

The same query by using the Graph Explorer works fine. I found some posts around that tells something, that you can not call the search API by using the application credential but only by using delegation. In my case the api call is made by a service user and not by the user directly. I have to migrate a Sharepoint on Premise solution which access the search in that way. Thanks for any input

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,495 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Shivam Dhiman 5,946 Reputation points
    2022-10-26T16:44:17.65+00:00

    Hi @Christoph Piock-Ellena

    As per the documentation, Search entity Graph API is only available in Delegated permission.
    254337-search.png

    Its working in Graph Explorer because Graph explorer only supports Delegated permissions.

    Hope this helps.
    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    0 comments No comments

  2. Christoph Piock-Ellena 1 Reputation point
    2022-10-27T06:59:21.29+00:00

    @ShivamDhiman-1582 ok, but yesterday I found out, that since 10.10.2022 on the beta endpoint should work as preview the application permission for Site.Read.All. Tried also on beta endpoint but I get the same error. What I do wrong?

    https://developer.microsoft.com/en-us/graph/changelog/?search=appli
    https://learn.microsoft.com/en-us/graph/api/search-query?view=graph-rest-beta&tabs=http