0

I am using Microsoft Graph API and I am trying to get Drive items that are inside a folder, this is what I got so far, I got most of this code from this Microsoft tutorial.

I get the drive items like so:

public static async Task<IEnumerable<DriveItem>> GetFilesAsync()
{
    var graphClient = GetAuthenticatedClient();
    var files = await graphClient.Me.Drive.Request().GetAsync();
    return files.Items.CurrentPage;
}

And I call this method in my main controller:

[Authorize]
public async Task<ActionResult> Index()
{
    var files = await GraphHelper.GetFilesAsync();    
    return View(files);
}

And then my view:

@model IEnumerable<Microsoft.Graph.DriveItem>    
@{
    ViewBag.Current = "Drive";
}    
<table class="table">
    <thead>
        <tr>
            <th scope="col">Name</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Name</td>
            </tr>
        }
    </tbody>
</table>

But when I run this code, I get this error:

Object reference not set to an instance of an object.

According to the stack, the error happens here:

var files = await graphClient.Me.Drive.Request().GetAsync();

What I am trying to accomplish is getting Files from my OneDrive that are in a certain folder.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
user979331
  • 11,039
  • 73
  • 223
  • 418
  • which line of code throws the error? What debugging have you done in order to try and determine which piece of data is null when it shouldn't be? – ADyson Nov 13 '19 at 14:14
  • Thats what I am trying to find out now, this project is super hard to debug on my localhost as I have to sign in – user979331 Nov 13 '19 at 15:29
  • well your stack trace should at least tell you the line. Why does signing in make it hard to debug though? That isn't immediately clear. – ADyson Nov 13 '19 at 15:34
  • According to the stack, the error happens here: var files = await graphClient.Me.Drive.Request().GetAsync(); – user979331 Nov 13 '19 at 15:55
  • Ok. But that still leaves the possibilty (in theory) that any of `graphClient`, `Me`, `Drive`, or the result of `Request()` could be the offending NULL value. You either need to run the debugger and pause on the error, and then examine the object to see what's null, or you need to split that into separate lines to assign each property to a separate variable, and then it will break on the exact line when it first encounters a null object. Also you still didn't say why signing in makes debugging difficult. – ADyson Nov 13 '19 at 15:57
  • Please see also [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ADyson Nov 13 '19 at 15:59
  • This issue is super annoying because I know there are items in my drive, but I cant get them, so stupid. – user979331 Nov 13 '19 at 16:27
  • Ok. I sympathise, I'm sure it's very frustrating, but understanding how you feel about it doesn't help us to fix your issue. Can we stick to information about the code? It's not really a chatroom, if I'm being honest. – ADyson Nov 13 '19 at 16:38
  • This line solved my problem....var files = await graphClient.Me.Drive.Root.Children.Request().GetAsync(); – user979331 Nov 13 '19 at 16:48
  • That's great. But if you found the answer, please place it in the "answer" box below...you're allowed to answer your own question. And then people can vote it up if they find it useful or informative, and others with a similar problem can find it in search results (comments are not indexed, but answers are). I note from your profile you've asked and answered a large number of questions previously so I'd expect you might already understand the best way to share the solution? Cheers. – ADyson Nov 13 '19 at 17:02

1 Answers1

1

Your code is simply requesting the Drive resource:

await graphClient
    .Me
    .Drive
    .Request()
    .GetAsync();

Microsoft Graph (and really, REST APIs in general) are very literal, they only return exactly what you request. So if you request a container (Drive), it does not automatically return objects within that container (DriveItem).

In order to receive a collection of DriveItems, you need to request a folder and it's children. For example, to get the children of the root folder you would need:

await graphClient
    .Me
    .Drive // Your default drive
    .Root // The root of that drive
    .Children // The children of "root"
    .Request() // Build the request
    .GetAsync(); // Execute the request
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63