Search code examples
c#azureazure-active-directorymicrosoft-graph-apimicrosoft-graph-plannertasks

Retrieving a list of Planner plans via Microsoft Graph - exception message says undocumented filter is required


Yet again I find myself needing help with the Microsoft Graph API because the documented methods don't work. The Microsoft Graph Explorer offers the following URL for obtaining a list of all plans in a group:

https://graph.microsoft.com/v1.0/planner/plans

Furthermore, as the Graph Explorer and the documentation points out, the C# method for retrieving the same result is:

var result = await graphClient.Planner.Plans.GetAsync();

However, when using the Graph Explorer or the C# method I receive the following exception message:

This entity set must be queried with a filter on owner property, or container type and container external id, or contextScenarioId

Neither the Graph Explorer nor the documentation make any mention of filtering, and I can find no examples of how I should filter (format? passed to which property?) if I want to retrieve a list of all plans within a group - the group ID is specified in the GraphClient instance.

Can anyone help me retrieve a list of all plans within the group?

UPDATE:
Through trial and error, I've modified my code to this:

var response = await GraphClient.Value.Planner.Plans.GetAsync(requestConfig =>
                                                              {
                                                                  requestConfig.QueryParameters.Filter = $"Owner eq '{config.GroupId}'";
                                                              })

And now I get...

You do not have the required permissions to access this item.

What more permissions need I grant in order to list the plans? I'm already able to retrieve a plan by ID.

UPDATE 2:
Here is a list of permissions granted to my application (and all grants have type Application):

  • Group.Create
  • Group.ReadAll
  • Group.ReadWrite.All
  • GroupMember.Read.All
  • GroupMember.ReadWrite.All
  • Tasks.Read.All
  • Tasks.ReadWrite.All
  • User.ReadBasic.All

Solution

  • Initially, I too got same error when I ran below API call in Graph Explorer:

    GET https://graph.microsoft.com/v1.0/planner/plans
    

    Response:

    enter image description here

    As mentioned in this MS Document, listing planner plans requires owner filter to be specified.

    To resolve the error, you need to add $filter to the API call for listing planner plans within group like this:

    GET https://graph.microsoft.com/v1.0/planner/plans?$filter=owner eq 'groupID'
    

    Response:

    enter image description here

    Alternatively, you can directly use below API call to list all plans within a group:

    GET https://graph.microsoft.com/v1.0/groups/groupID/planner/plans
    

    Response:

    enter image description here

    To get the same response via c#, you can make use of below sample code by granting Tasks.Read.All permission of Application type:

    using Azure.Identity;
    using Microsoft.Graph;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = "tenantID";
            var clientId = "appID";
            var clientSecret = "secret";
    
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            };
    
            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
            try
            {
                var plans = await graphClient.Groups["groupID"].Planner.Plans.GetAsync();
    
                foreach (var plan in plans.Value)
                {
                    Console.WriteLine($"Plan Title: {plan.Title}");
                    Console.WriteLine($"Plan ID: {plan.Id}");
                    Console.WriteLine();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"{exception.GetType().FullName}: {exception.Message}");
            }
        }
    }
    

    Response:

    enter image description here

    Reference: List plans in group - Microsoft Graph v1.0