Seb
Seb

Reputation: 11

How to programmatically provision Personal Site (OneDrive) with Microsoft Graph API (C#)

When I try to retrieve newly created user's drive, I receive a

"User's mysite not found." error (Code: ResourceNotFound)

Users are created on our On premise AD, then synchronized in Azure. I've built a console app program in C# that can assign user licences programmatically (based on our SharePoint On-Premise home made IAM solution) but I also need to pre-provision OneDrive Personal Site for those users.

We know how to perform this through Powershell script but we need to use MS Graph API

await graphServiceClient.Users[USER_EMAIL].Drive.Request().GetAsync();

This code throw the following error:

"User's mysite not found."
Code: ResourceNotFound

Upvotes: 0

Views: 2088

Answers (2)

GJBisschop
GJBisschop

Reputation: 345

I've got this working in C# without using Powershell. Below is a dummy method that shows how. The personal site of 1 user is provisioned (the user needs to have the license to use OneDrive). The method purely shows the steps that need to be taken to accomplish this. The implementation and retry won't work in a live scenario with lots of emailIds. Creating the personal sites might take up to 24 hours.

The implementation is mainly based on what I found here: https://blogs.msdn.microsoft.com/frank_marasco/2014/03/25/so-you-want-to-programmatically-provision-personal-sites-one-drive-for-business-in-office-365/

Required NuGet packages:

Microsoft.SharePointOnline.CSOM

Polly

Dummy method:

private Polly.Retry.AsyncRetryPolicy WithRetry = Policy.Handle<ServiceException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(retryAttempt, 2)));
public async Task InitializeOneDriveForUser()
{
    var tenantAdminUrl = "https://<yourdomain>-admin.sharepoint.com";  //Replace <yourdomain> with your domain.
    var adminUserName = "youradminaccount@yourdomain";
    var adminPassword = "youradminpassword";

    var emailAddress = "youruser@yourusersdomain";
    var emailIds = new string[] { emailAddress };
    
    // Create personal site using CSOM. Maximum of 200 emailIds at a time. Might take up to 24 hours before all personal sites are created.
    using (var context = new Microsoft.SharePoint.Client.ClientContext(tenantAdminUrl))
    {
        var secureString = new System.Security.SecureString();
        foreach (char c in adminPassword)
        {
            secureString.AppendChar(c);
        }
        context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(adminUserName, secureString);
        context.ExecuteQuery();

        var profileLoader = Microsoft.SharePoint.Client.UserProfiles.ProfileLoader.GetProfileLoader(context);
        profileLoader.CreatePersonalSiteEnqueueBulk(emailIds);
        profileLoader.Context.ExecuteQuery();
    }

    // Confirm that the personal site exists with a call to root. If this throws an exception, retry. The user's one drive is provisioned by a succesfull call.
    var drive = await WithRetry.ExecuteAsync(() => this.graphClient.Drives[emailAddress].Root.Request().GetAsync());

    // Get the user's OneDrive to double-check that everything is correctly provisioned and the "User's mysite not found." error is not thrown anymore.
    var usersOneDrive = await this.graphClient.Users[emailAddress].Drive.Request().GetAsync();
}

At this moment creating a personal site for 1 user took less than a minute. If this takes longer you will get the ServiceException below after the 5 retry attempts. To solve this, increase the number of retry attempts.

Code: invalidRequest Message: The provided drive id appears to be malformed, or does not represent a valid drive.

Inner error

Upvotes: 2

FerronSW
FerronSW

Reputation: 535

You can't! You can only do this with PowerShell:

https://learn.microsoft.com/en-us/onedrive/pre-provision-accounts

You can execute PowerShell cmdlets with C# if you have too.

Upvotes: -1

Related Questions