0
votes

I have an app that processes a bunch of data and generates some results. Currently the app emails the results, but this can be cumbersome and the email can get too big, so I'm looking to have the app store the results to a shared OneDrive folder. The app runs without user interaction.

I've been looking into multiple samples for the Microsoft.Graph sdk. I'v been able to authenticate using an application and the ConfidentialClient workflow, but I'm not sure how to find/access a shared directory in OneDrive. Currently just the root drive request doesn't return any children or anything useful. I could access the shared drive when I used the API to login as my user, but that was using a share link for my user. Do I need to generate a share link somehow for the app or not tied to a user? Or is there some other way to find a shared drive?

Here's the code creating my GraphServiceClient:

public static GraphServiceClient GetAuthenticatedClient()
        {
            if (graphClient == null)
            {
                ConfidentialClientApp = ConfidentialClientApplicationBuilder.Create(clientId).WithTenantId(FormBrowser.MsaTenantId).WithClientSecret(FormBrowser.MsaClientSecret).Build();
                ClientCredentialProvider authProvider = new ClientCredentialProvider(ConfidentialClientApp);
                graphClient = new GraphServiceClient(authProvider);
            }
            return graphClient;
        }

Then I've tried some of these different calls just to make sure it is authenticating correctly:

   //var shares = await this.graphClient.Shares[encodedUrl].Root.Request().Expand(expandValue).GetAsync();
                    //ProcessFolder(shares);
                    var drive = graphClient.Drive.Request().GetAsync();
                    ProcessFolder(await this.graphClient.Drive.Root.Request().Expand(expandValue).GetAsync());

Here's a sample JSON response from the Drive.Root request:

{
    "createdDateTime": "2013-11-07T19:59:00+00:00",
    "lastModifiedDateTime": "2019-09-15T02:12:23+00:00",
    "name": "root",
    "webUrl": "https://<company>.sharepoint.com/Documents",
    "fileSystemInfo": {
        "createdDateTime": "2013-11-07T19:59:00+00:00",
        "lastModifiedDateTime": "2019-09-15T02:12:23+00:00"
    },
    "folder": {
        "childCount": 0
    },
    "parentReference": {
        "driveId": "<stuff>",
        "driveType": "documentLibrary"
    },
    "root": {

    },
    "size": 0,
    "children": [

    ],
    "thumbnails": [

    ],
    "id": "<stuff>",
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drive/root(thumbnails(),children(thumbnails()))/$entity",
    "[email protected]": "https://graph.microsoft.com/v1.0/$metadata#drive/root/children(thumbnails())",
    "[email protected]": "https://graph.microsoft.com/v1.0/$metadata#drive/root/thumbnails",
    "responseHeaders": {
        "Transfer-Encoding": [
            "chunked"
        ],
        "Vary": [
            "Accept-Encoding"
        ],
        "request-id": [
            "<id>"
        ],
        "client-request-id": [
            "<id>"
        ],
        "x-ms-ags-diagnostic": [
            "{\"ServerInfo\":{\"DataCenter\":\"North Central US\",\"Slice\":\"SliceC\",\"Ring\":\"1\",\"ScaleUnit\":\"000\",\"RoleInstance\":\"<stuff>\",\"ADSiteName\":\"<stuff>\"}}"
        ],
        "OData-Version": [
            "4.0"
        ],
        "Duration": [
            "259.0577"
        ],
        "Strict-Transport-Security": [
            "max-age=31536000"
        ],
        "Cache-Control": [
            "private"
        ],
        "Date": [
            "Thu, 19 Sep 2019 14:06:27 GMT"
        ]
    },
    "statusCode": "OK"
}
1

1 Answers

1
votes

So I was able to get there through a round-about way, if someone knows an easier way I'd really appreciate it. Here are the steps I took:

1) Authenticated with my user and loaded the info using the sharing url:

 string sharingUrl = "<url>";
 string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
 string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');                        
 var shares = await this.graphClient.Shares[encodedUrl].Root.Request().Expand(expandValue).GetAsync();

Once I got the response for that I noted the "driveId" for the drive. Then when I authenticate using my confidentialclient, I can specify the drive in my request:

await this.graphClient.Drives["<driveId from 1>"].Root.Request().Expand(expandValue).GetAsync()

I'm wondering if there's an easier way to find those driveId's, like from the sharepoint site?

Also, it looks like when I get the sharing link from Sharepoint, if I switch the link from "specific people" to "People in " then I can use the Shares to get the drive items.