0

I am looking to figure out a way to get the ID of a user, given their name, so that I can use it in a call to /_layouts/15/userdisp?ID=n, where 'n' is the ID of the user to redirect the person who is viewing the page to another person's Profile page. I am using JavaScript. I will not know the url of the mysites site hence using this method rather than the one using the MySites method. I am on a search results page not a person search results page.

I am using SharePoint 2013 SP1 April 2015 CU.

3 Answers 3

3

you can use:

<script>
    var userId = _spPageContextInfo.userId;
</script>

_spPageContextInfo is your new best friend

5
  • 1
    Keep in mind that the user profile url requires the claims encoded login name (at least O365 does), and that _spPageContextInfo returns the user's id for that particular site collection. edit: just noticed that this is not the my site url he's looking for. Great answer!
    – wjervis
    Apr 30, 2015 at 18:59
  • 1
    is _spPageContextInfo.userId not the id of the user who is browsing the page not the id of a particular user ? Apr 30, 2015 at 19:56
  • pls can you provide more details? I did not understand
    – Nk SP
    Apr 30, 2015 at 20:33
  • _spPageContextInfo.userID is the ID of the current user. ie the person who is browsing the page Apr 30, 2015 at 22:38
  • 1
    The questions how to get the ID of another user (not the currently logged in user) May 1, 2015 at 9:35
3

I would suggest to do not reinvent the wheel resolve personal site url from user login name directly instead of constructing the url to display form of User Information list (/_layouts/15/userdisp?ID=<user id>).

The following example demonstrates how to retrieve user personal site url:

function navigateToPersonalSite()
{

  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(){

    getUserProperies(loginName,
       function(properties){
          var url = properties.get_personalUrl();
          window.location.href = url;
       }, 
       function(sender, args) {
          //handle errors...
       }
    );

   });
}   



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

Usage

var loginName = "i:0#.f|membership|[email protected]";
navigateToPersonalSite(loginName);
6
  • Thanks Vadim - I need the url because it is a link to the personal details where, if the usewr wants to, they can click on it and go to the personal details. otherwise the link just stays on the page, May 1, 2015 at 8:28
  • Hi Vadim - do we not need promises here if we just want the url, and not to redirect to another site ? May 1, 2015 at 11:09
  • 1
    Since executeQueryAsync is async method, it could be invoked as a promise or callback as in my case May 1, 2015 at 11:37
  • do you have an example as a promise ? May 1, 2015 at 12:13
  • At the moment the code is going through twice, once with no results and then later when the query has finished with the results in. So on my screen it comes out as a null url. May 1, 2015 at 12:14
1

You can use REST:

var getUserId = function() {
    return $.ajax({
        url: "<site url>/_api/web/siteusers?$select=Id&$filter=Title eq '<user's name>'",
        type: 'GET',
        headers: {
            'accept': 'application/json;odata=nometadata', // JSON Lite should be available in SP1.  If not, use odata=verbose instead
            'content-type': 'application/json;odata=nometadata' // same thing, replace with odata=verbose if this doesn't work
        }
    });
}

var userId = '';
getUserId().done(function(d){
    userId = d.value[0].Id;
    var userDispUrl = _spPageContextInfo.siteAbsoluteUrl + '/_layouts/15/userdisp?ID=' + userId;
    window.location.href = userDispUrl; // or do whatever you want with the ID, but do it in this callback.
});

This focuses on the users, groups and roles REST API.

Let me know if you have questions.

4
  • It works fine but.... Apr 30, 2015 at 23:18
  • But...? Is there something you don't understand?
    – wjervis
    Apr 30, 2015 at 23:21
  • Yes why, when I step from getUserId().done(function(d){userId=d[0].Id;}) do I lose the value of userId ? and cannot use it elsewhere. May 1, 2015 at 8:13
  • See my edit. You can either make userId a globally scoped variable (excessive), or simply do what you need to in the callback function.
    – wjervis
    May 1, 2015 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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