Dylan52
Dylan52

Reputation: 61

Microsoft Graph API To Do List Call (Cannot Convert ITodoListCollectionPage to Todo)

I am creating a .Net Core application in which a user can login/logout with their microsoft 365 account and also view their Task Lists/Tasks via the Microsoft Graph API. I am currently trying to request the list via the API using.

Controller.cs

 [AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
        public async Task<IActionResult> Tasks()
        {
            User currentUser = null;
            Todo currentLists = null;

            try 
            {
                currentUser = await _graphServiceClient.Me.Request().GetAsync();
                currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();

            }
            catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))
            {
                try
                {
                    Console.WriteLine($"{svcex}");
                    string claimChallenge = WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
                    _consentHandler.ChallengeUser(_graphScopes, claimChallenge);
                    return new EmptyResult();
                }
                catch (Exception ex2)
                {
                    _consentHandler.HandleException(ex2);
                }
            }

            ViewData["Me"] = currentUser;
            ViewData["Lists"] = currentLists;
            return View();
        }

However this throws an issue where

currentLists = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();

Cannot convert to the Todo type in order for me to then use the data on the following cshtml page

@using Newtonsoft.Json.Linq
@{
    ViewData["Title"] = "User Profile fetched from MS Graph";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />

<table class="table table-striped table-condensed" style="font-family: monospace" border="1">
    <thead>
        <tr>
        </tr>
        @{
            var lists = ViewData["lists"] as Microsoft.Graph.TodoTaskList;
            <tr>
                <th>
                    ID
                </th>
                <th>
                    @lists.DisplayName
                </th>
                <th></th>
            </tr>
        }
    </thead>
    <tbody>
    </tbody>

Where converting currentLists to the type suggested "ITodoListCollectionsPage" causes the web application to now allow access to the data requested.

How can I fix this and call the Microsoft Graph API to get the Todo Task Lists and output that correctly

Upvotes: 1

Views: 702

Answers (1)

user2250152
user2250152

Reputation: 20635

await _graphServiceClient.Me.Todo.Lists.Request().GetAsync(); returns ITodoListCollectionsPage.

You need to iterate through all pages and get all ToDo task lists.

var currentLists = new List<TodoTaskList>();
var todoListsPage = await _graphServiceClient.Me.Todo.Lists.Request().GetAsync();
currentLists.AddRange(todoListsPage.CurrentPage.ToList());
while (todoListsPage.NextPageRequest != null)
{
    todoLists = await todoListsPage.NextPageRequest.GetAsync();
    currentLists.AddRange(todoListsPage.CurrentPage.ToList());
}

If you want to get todo tasks you need to make another request for each TodoTaskList in List<TodoTaskList>

Only example how to get todo tasks:

var todoTasksDict = new Dictionary<string, List<TodoTask>>();
foreach (var todoList in currentLists)
{
    var todoTasks = new List<TodoTask>();
    var todoTasksPage = await client.Me.Todo.Lists[todoList.Id].Tasks.Request().GetAsync();
    todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
    while (todoTasksPage.NextPageRequest != null)
    {
        todoTasksPage = await todoTasksPage.NextPageRequest.GetAsync();
        todoTasks.AddRange(todoTasksPage.CurrentPage.ToList());
    }
    todoTasksDict[todoList.DisplayName] = todoTasks;
}

I don't know how do you want to display todo task lists and related todo tasks but you will need to modify your views.

Upvotes: 1

Related Questions