6

I am trying to get the URL of the logged in user's Personal site from another site collection. I am working on SharePoint 2013 online and using ECMA.

4

Use the below code

function getPersonalUrl() {
var context = SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(context);
userProfileProperties = peopleManager.getMyProperties();
context.load(userProfileProperties, 'PersonalUrl');
context.executeQueryAsync(success, fail);
}
function success(){
  var mySiteUrl = userProfileProperties.get_personalUrl();
}

Make sure SP.UserProfiles.js is loaded and user had read access to user profile in appmanifest.xml.

2

Garima's answer is fine but the problem is the PersonalUrl can be overwritten by the user. It remains as MySite Url when its pulled back in the code if its blank but if the user enters a value you get back that value instead of the MySite Url. A better function to use is get_userUrl().

So I'd change the above code to be:

function getPersonalUrl() {
    var context = SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(context);
    userProfileProperties = peopleManager.getMyProperties();
    context.load(userProfileProperties, 'PersonalUrl');
    context.executeQueryAsync(success, fail);
}

function success(){
    var mySiteUrl = userProfileProperties.get_userUrl();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.