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