Creating My Site through JavaScript



With growing influence of JavaScript in our day to day programming we try to accomplish as much through JavaScript. SharePoint is no different.

So what if you want to create My Site for your user using JavaScript. I found 2 very interesting and content rich articles which you would want to read to understand the concepts and ways to create and brand my sites in your installation; you can find Vesa’s blog here and Frank Marasco’s one here. To know more about the available APIs you can look at this MSDN article, which is basically what you need to work with User Profiles in SharePoint.

Get The Ball Rolling

Once you have read all 3 articles, you have almost got the feel of what you need to play around with User Profiles to create/ brand My Sites(Personal Site, some would say). You would require ProfileLoader and PropleManager classes to accomplish the task. So without wasting much time, I would jump in to the JavaScript code which would let you create My Sites for your user(s).

Complete Code

var SHIV = SHIV || {};
SHIV.MySite = {
    clientContext: "",
    personProperties: "",
    personalSite: "",
    profile: "",

    GetUserProperties: function () {

        var deferred = $.Deferred();
        SHIV.MySite.clientContext = SP.ClientContext.get_current();

        //Get the People Manager
        var peopleManager = new SP.UserProfiles.PeopleManager(SHIV.MySite.clientContext);

       //getMyProperties takes current logged user. Use peopleManager.getPropertiesFor method for a different user
        SHIV.MySite.personProperties = peopleManager.getMyProperties();

            Function.createDelegate(this, function () { deferred.resolve(SHIV.MySite.personProperties); }),
            Function.createDelegate(this, function (sender, args) { deferred.reject(sender, args); }));

        return deferred.promise();
    CreateMySite: function () {
            function (personProperties) {
                var personalUrl = personProperties.get_personalUrl();
                if ((personalUrl !== undefined) || (personalUrl.indexOf("personal") === -1)) {

                    var loader = new SP.UserProfiles.ProfileLoader.getProfileLoader(SHIV.MySite.clientContext);
                    SHIV.MySite.profile = loader.getUserProfile();

                    // Load the profile object and send the request.
                    SHIV.MySite.clientContext.executeQueryAsync(SHIV.MySite.GetProfileSuccess, SHIV.MySite.GetProfileFail);
                } else {
                    //Do some operation if the My Site is already present or just halt


            function (sender, args) {
                console.log('An error occurred while retrieving user properties:' + args.get_message());

    GetProfileSuccess: function () {
        SHIV.MySite.clientContext.executeQueryAsync(SHIV.MySite.CreateMySiteSuccess, SHIV.MySite.CreateMySiteFail);
    GetProfileFail: function (sender, args) {
        console.log('An error occurred while retrieving profile:' + args.get_message());
    CreateMySiteSuccess: function () {
        console.log("Request for My Site creation has been placed in the queue. Please check back later");
    CreateMySiteFail: function (sender, args) {
        console.log('An error occurred while creating my site' + args.get_message());

How to use this in your code

You can drop in a Script Editor web part on a page where you want to invoke this code and reference jQuery, the .js file having the above code, sp.runtime.js, sp.js and sp.userprofile.js.

Once you have all the required JavaScript files you just need to call the CreateMySite function. For Example:


That’s it.


If you have read the articles mentioned in the Overview section then you must have slid through it easy. However, I will briefly explain you what I am doing here.

The execution will start with CreateMySite() function which calls GetUserProperties(). GetUserProperties() function gets hold of PeopleManager and invokes getMyProperties() method.

This method is to get current user properties. If you want to get properties for some other user then you need to call getPropertiesFor passing in the user’s login name. In case of on-prem it would be DomainUserName and in office 365 it would be similar to i.e. peopleManager.getPropertiesFor(userAccount)

Once I have the user properties I get the personalUrl i.e. personProperties.get_personalUrl(). Then I check if the personal url is of the format that I have set up in the User Profile Service Application. In my case all the My Sites have been set up to be created under /Personal managed path which is ‘Wildcard Inclusion’ managed path.

If I find that the My Site is not present then I get hold of Profile Loader and invoke the method to get the user profile of the account.

var loader = new SP.UserProfiles.ProfileLoader.getProfileLoader(SHIV.MySite.clientContext);
SHIV.MySite.profile = loader.getUserProfile();

This will get me the user profile which has the method to invoke the timer job, which in the background creates My Site.

SHIV.MySite.clientContext.executeQueryAsync( SHIV.MySite.CreateMySiteSuccess, SHIV.MySite.CreateMySiteFail);

Passing true to createPersonalSiteEnque method invokes a timer job of higher priority, assuming that this is done through UI and false will invoke another timer job which is of lesser priority than the one invoked through the UI (user visiting the My Site host).

That’s all for now. Have a good rest of your day!