Search code examples
c#microsoft-graph-api

GetConfig not working for Microsoft Graph


Developing APIs that consumes Microsoft Graph SDK to get list of Teams for a tenant. The code below works: `

_clientSecretCredential = new ClientSecretCredential("TenantId", "ClisntId", "Secreat");
_appClient = new GraphServiceClient(_clientSecretCredential,
// Use the default scope, which will request the scopes
// configured on the app registration
new[] { "https://graph.microsoft.com/.default" });
            TeamCollectionResponse _rsp = new TeamCollectionResponse();

            return await _appClient.Teams.GetAsync();

This returns all the fields of Teams. However, when I try to use config to get only selected fields the Graph is giving ODATA error, here is the code:

_clientSecretCredential = new ClientSecretCredential("TenantId", "ClisntId", "Secreat");
_appClient = new GraphServiceClient(_clientSecretCredential,
// Use the default scope, which will request the scopes
// configured on the app registration
new[] { "https://graph.microsoft.com/.default" });
            TeamCollectionResponse _rsp = new TeamCollectionResponse();

            return await _appClient.Teams.GetAsync((config) =>
            {
                // Only request specific properties
                config.QueryParameters.Select = new[] { "displayName", "id" };
                // Get at most 25 results
                config.QueryParameters.Top = 2;
                // Sort by display name
                config.QueryParameters.Orderby = new[] { "displayName" };
            });

This is from: https://learn.microsoft.com/en-us/graph/api/teams-list?view=graph-rest-1.0&tabs=http

I tried other fields but nothing worked.

Error: Microsoft.Graph.Models.ODataErrors.ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown. at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response, Dictionary2 errorMapping, Activity activityForAttributes) at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory1 factory, Dictionary2 errorMapping, CancellationToken cancellationToken) at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory1 factory, Dictionary2 errorMapping, CancellationToken cancellationToken) at Microsoft.Graph.Teams.TeamsRequestBuilder.GetAsync(Action1 requestConfiguration, CancellationToken cancellationToken) at SmartUCC.API.Controllers.TeamsController.GetAllTeams(String tenantId) in C:\Users\rupena\Documents\VerizonTeams\API\SmartUCC.API\SmartUCC.API\SmartUCC.API\Controllers\TeamsController.cs:line 80 at lambda_method6(Closure, Object) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)


Solution

  • The issue is that the endpoint doesn't support $orderby query parameter. When using $orderby the Graph API returns the following error

    GET https://graph.microsoft.com/v1.0/teams?$select=id,displayName&$top=25&$orderby=displayName
    
    {
        "error": {
            "code": "",
            "message": "The query specified in the URI is not valid. Query option 'OrderBy' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.",
            "details": [],
            "innerError": {
                "message": "Query option 'OrderBy' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.",
                "date": "2023-08-31T05:38:19",
                "request-id": "6e1bade7-3dd5-4e80-871f-8adbb4251880",
                "client-request-id": "c1d26c63-3c26-99d2-bc4f-a6e59a9325e7"
            }
        }
    }
    

    So, get rid of OrderBy from your code

    _clientSecretCredential = new ClientSecretCredential("TenantId", "ClisntId", "Secreat");
    _appClient = new GraphServiceClient(_clientSecretCredential,
    // Use the default scope, which will request the scopes
    // configured on the app registration
    new[] { "https://graph.microsoft.com/.default" });
            TeamCollectionResponse _rsp = new TeamCollectionResponse();
    
            return await _appClient.Teams.GetAsync((config) =>
            {
                // Only request specific properties
                config.QueryParameters.Select = new[] { "displayName", "id" };
                // Get at most 25 results
                config.QueryParameters.Top = 2;
            });