Auto provision OneDrive for users – programmatically

By default OneDrive storage is not provisions for users until they click on the OneDrive link at the top navigation bar in Office 365.

This can be a massive pain, especially if they’re using the iPad app which wont provision OneDrive instead it just repeatedly asks them to login! Ouch!

Fortunately there is a way to do this using CSOM. Firstly download the client runtime dlls from ​http://www.microsoft.com/en-us/download/details.aspx?id=42038

After installing, add the version 16 DLLs to a new c# console application in Visual Studio 2013 references from Program files/SharePoint Client Components.

Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;

namespace CreatePersonalSiteBulkConsole
{
class Program
{
static void Main(string[] args)
{
string userName = “administrator@contoso.onmicrosoft.com”;
string passwordStr = “password”;
string serverUrl = “https://contoso-admin.sharepoint.com/”;

using (var clientContext = new ClientContext(serverUrl))
{
SecureString password = new SecureString();
Array.ForEach(passwordStr.ToCharArray(), c => password.AppendChar(c));

var credentials = new SharePointOnlineCredentials(userName, password);

clientContext.Credentials = credentials;

var web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
ProfileLoader loader = ProfileLoader.GetProfileLoader(clientContext);

if (loader == null)
{
throw new InvalidOperationException(“Failed to get ProfileLoader”);
}

string[] userEmails = { “usera@contoso.onmicrosoft.com”, “userb@contoso.onmicrosoft.com” };
loader.CreatePersonalSiteEnqueueBulk(userEmails);
loader.Context.ExecuteQuery();
}
}
}
}