Search code examples
microsoft-graph-apionedrive

Access all user drives as Admin


I am building a native application that uses Microsoft Graph. I want to read all the files in OneDrive of all the users in the organization, via admin account.

I am using the (Azure AD 2.0) OAUTH authorization workflow to get the authorization code. After which I get the access token and refresh token.

However when I try to access the drive of any user :

graphClient.Drives["amit@csys.onmicrosoft.com"].Root.Request().GetAsync();
// where graphClient is instance of GraphServiceClient

I get:

generalException

Message: Unexpected exception returned from the service.

With call stack :

  at Microsoft.Graph.HttpProvider.<SendAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.BaseRequest.<SendAsync>d__32`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.DriveItemRequest.<GetAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at OneDrive_Writer.OneDriveWriter.<loadRootDriveFolder>d__9d.MoveNext()

Is there a way an admin can access all other users drives files?

NOTE: I am able to access the Admin's OneDrive account when I login as Admin.


Solution

  • The Drives indexer in your example expects a driveId, not a upn.

    Assuming that your permissions are properly set, and you have a valid access token, you'll do something like this.

    // Access the users in the org.
    var users = await graphClient.Users.Request().GetAsync();
    
    // Get the drives for a user.
    var drives = await graphClient.Users[users[0].Id].Drives.Request().GetAsync();
    
    // Get the specific drive metadata for a user
    var drive = await graphClient.Drives[drives[0].Id].Request().GetASync();