3

I need to get the current user MySite Url using javascript / jQuery. I am using SP 2013

4

This is just an expansion of @aveenavs answer, which works great for me, unless the user is on the root site of the application. The only difference is using _spPageContextInfo.webAbsoluteUrl instead of _spPageContextInfo.webServerRelativeUrl in the url in the ajax call. So I would change it to this:

//Get user's personal MySite url
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetUserProfileProperties);

function GetUserProfileProperties() {
    //Get the current user's account information
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=UserUrl",
        method: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            var userUrl = data.d.UserUrl;
            window.console && console.log(userUrl);
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}
  • Changing from _spPageContextInfo.webServerRelativeUrl to _spPageContextInfo.webAbsoluteUrl did the trick. Thanks! – gilango Dec 23 '16 at 22:37
3

Try this:

   var profileManager = new UserProfileManager(ServerContext.GetContext(SPContext.Current.Site));
    var profile = profileManager.GetUserProfile("domain\\username"); //instead of GetUserProfile use GetUserProfile(True);
    using (SPSite personalSite = profile.PersonalSite)
    {
         var personalSiteUrl = personalSite.PersonalUrl; 
    }

Also:

private string GetPersonalSite(SPSite site)
{
    var curUser = SPContext.Current.Web.CurrentUser;
    SPServiceContext context = SPServiceContext.GetContext(site); 
    UserProfileManager upm = new UserProfileManager(context);
    UserProfile profile = upm.GetUserProfile(curUser.LoginName);
    return profile.PersonalUrl.AbsoluteUri;
}
  • The OP asked for Javascript examples this is clearly C#. – P. Roe Aug 26 '16 at 21:42
3

You can use CSOM or REST api to get user's personal MySite url.

//Get user's personal MySite url
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetUserProfileProperties);

function GetUserProfileProperties() {
    //Get the current user's account information
    $.ajax({
        url: _spPageContextInfo.webServerRelativeUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=UserUrl",
        method: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            var userUrl = data.d.UserUrl;
            window.console && console.log(userUrl);
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}
  • lol, way to beg for imaginary internet points – Hristo Yankov Nov 17 '14 at 12:12
  • This should be marked as correct, since the other answer doesn't use JavaScript. But, it needs to be changes because it doesn't work on the root site (it adds an extra '/') – stinaq Feb 2 '15 at 11:24
1

The following examples demonstrate how to retrieve personal site url using SharePoint 2013/Online CSOM API:

Option 1

function getPersonalSiteUrl(loginName,success,failed)
{
   var ctx = new SP.ClientContext.get_current();
   var peopleManager = new SP.UserProfiles.PeopleManager(ctx);
   var property = peopleManager.getUserProfilePropertyFor(loginName,'PersonalSpace');
   ctx.executeQueryAsync(
       function(){
          success(property.get_value());
       }, 
      failed);
}       

Usage

SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
 SP.SOD.registerSod('SP.UserProfiles.PeopleManager', SP.Utilities.Utility.getLayoutsPageUrl('sp.userprofiles.js'));
 SP.SOD.loadMultiple(['SP.ClientContext', 'SP.UserProfiles.PeopleManager'], function(){

    getPersonalSiteUrl(_spPageContextInfo.userLoginName,
       function(url){
          console.log(url);
       }, 
       function(sender, args) {
          //handle errors...
       }
    );

 });

Option 2

function getMyUserProperies(success,failed)
{
   var ctx = new SP.ClientContext.get_current();
   var peopleManager = new SP.UserProfiles.PeopleManager(ctx);
   var properties = peopleManager.getMyProperties();
   ctx.load(properties);
   ctx.executeQueryAsync(
       function(){
          success(properties);
       }, 
      failed);
}       

Usage

 SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
 SP.SOD.registerSod('SP.UserProfiles.PeopleManager', SP.Utilities.Utility.getLayoutsPageUrl('sp.userprofiles.js'));
 SP.SOD.loadMultiple(['SP.ClientContext', 'SP.UserProfiles.PeopleManager'], function(){

    getMyUserProperies(
       function(properties){
          var url = properties.get_personalUrl();
          console.log(url);
       }, 
       function(sender, args) {
          //handle errors...
       }
    );

 });
  • Is there a way to find the OneDrive url (personal site URL) for a user by specifying just the email? For example, there's a tenant user abc@mytenant.onmicrosoft.com, is there an API call that would tell me the personal site url for this user? I use Client Credentials Flow to get the access token so I don't have a user context in the tokens. – Syed Mauze Rehan Jan 4 '16 at 7:42
  • ** other than the constructing the URL from the email which I am aware of. is there an API call which retrieves that info? – Syed Mauze Rehan Jan 4 '16 at 7:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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