Deciphering ODataError

Paul Smith 20 Reputation points
2023-09-19T06:11:28+00:00

Hello:

I am using C# to download a user's Todo tasks. I have delegated permission from the user. The code is:


            if (_userClient == null)
            {
                throw new NullReferenceException("Graph has not been initialized for user authorization");
            }

            TodoTaskCollectionResponse result = new TodoTaskCollectionResponse();

            try
			{            
                result = await _userClient.Me.Todo.Lists[listID].Tasks.GetAsync();
            }

            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

I must be doing something right, because this works about two-thirds of the time, but the other third I get an OData Error. The occurrence appears to be random. The error message is attached. Is there any way of telling from the error message what went wrong? If not, can you suggest an approach to debugging this?

Thanks for any advice you can give me.

Paul

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
2,056 questions
Microsoft Graph to Do Tasks API
Microsoft Graph Users API
Microsoft Graph Users API
A Microsoft API that allows you to build compelling app experiences based on users, their relationships with other users and groups, and the resources they access for example their mails, calendars, files, administrative roles, group memberships.
870 questions
Microsoft Graph SDK
Microsoft Graph SDK
A Microsoft software developer kit designed to simplify building high-quality, efficient, and resilient applications that access Microsoft Graph.
990 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 28,196 Reputation points
    2023-09-19T08:17:35.45+00:00

    Hi @Paul Smith

    Make sure you are using Graph .NET SDK version 5.0 or above.

    Also, your error is not necessarily a service exception, it could be specific instances of ODataError exceptions thrown by the SDK that rely on metadata provided by the API to return valuable details, so try to catch the ODataError exception.

    try
    {
        result = await graphClient.Me.Todo.Lists["{list id}"].Tasks.GetAsync();   
    }
    
    catch (ODataError odataError)
    {
        Console.WriteLine(odataError.Error.Code);
        Console.WriteLine(odataError.Error.Message);    
    }
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Paul Smith 20 Reputation points
    2023-09-19T06:14:10.59+00:00

    I thought I uploaded the error message. Trying again . . ODataError.png