6

In sp2013 restful call, I have this link

http://mysite/_api/sp.userprofiles.peoplemanager/getuserprofilepropertyfor(accountname=@v, propertyname='Office')?@v='aq\test'

And this works with managed properties, but I don't want to supply the actual username for the current user. Is there some variable like @me or something I can use to mean the current user?

Thanks

7
  • What about: mysite/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Office Commented Aug 18, 2015 at 20:37
  • 1
    Doesn't work with custom properties/ but the one I have works
    – omega
    Commented Aug 18, 2015 at 20:43
  • Where are you using the REST call? If it's JS, you don't need to supply the username, you can use var userid = _spPageContextInfo.userId; and pass that userid Commented Aug 18, 2015 at 20:45
  • do you mean accountname=@v where @v is the user id number?
    – omega
    Commented Aug 18, 2015 at 20:48
  • I tried that, it didn't work. Can you show it as an example url, maybe my syntax is off.
    – omega
    Commented Aug 18, 2015 at 20:50

3 Answers 3

5

Although I couldn't find a way to retrieve the custom user profiles by using the user profiles REST api since the properties are returned in an array and AFAIK no existing method exists to filter them from the URL but since some of the OOB user profile properties are stored to the user information list, you can maybe try this:

var userId = _spPageContextInfo.userId;
var url = "https://siteUrl/_api/web/lists/getbytitle('User Information List')/items("+userId+")?$select=Office"

`

1
  • 1
    I love the simplicity of a single line to get the user id _spPageContextInfo.userId Commented Aug 31, 2017 at 3:04
1

Try http://site/_api/web/currentuser. This should return the current logged in user that is used to send the REST query.

1
  • This works but still requires me to make 2 ajax calls. One to get username then one to get managed property...
    – omega
    Commented Aug 19, 2015 at 13:13
0

If you want to get user profile properties for a current user via REST API, then you can either pass user id and get all properties including your own custom or use method GetMyProperties.

Code where you pass userId.

userInfo.UserProfileProperties.results[10] is a bit of code where you get available user profile properties. If I am not wrong then custom properties will be at the last indexes like 60, 61 this you need to identify.

function getUserInfoSuccess (data) {
    var userInfo = data.d;
    alert(userInfo.UserProfileProperties.results[10]);     
}

function getUserName() {
    var clientContext = SP.ClientContext.get_current(),
        userAccount = clientContext.get_web().getUserById(_spPageContextInfo.userId);

    clientContext.load(userAccount); 
    clientContext.executeQueryAsync(function () {

        var index = userAccount.get_loginName().indexOf('|') + 1,
            loginName = userAccount.get_loginName().substring(index);

        jQuery.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/getpropertiesfor(@v)?@v='" + loginName + "'",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose"
            },
            success: function (data) {
                getUserInfoSuccess(data);
            },
            error: function () {
                alert("Failed to get user. Please refresh page.");
            }
        });
    },
        function () {
            alert("Failed to get user. Please refresh page.");
        });
}

Usage of GetMyProperties

1) Get all properties of current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

2) Get single property of current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/PictureUrl

OR

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl

3) Get Multiple Properties for the current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,AccountName

For more details refer SharePoint 2013: Get UserProfile Properties with REST API

5
  • like I said in my first post, this doesn't work with custom managed properties...
    – omega
    Commented Aug 19, 2015 at 13:09
  • @omega : I have updated my answer with code which I used to retrieve custom properties.
    – Asad Refai
    Commented Aug 21, 2015 at 4:44
  • This looks like 2 ajax calls
    – omega
    Commented Aug 21, 2015 at 4:48
  • @omega : Yes in getUserName first you get useraccount by userid, then with useraccount you make a call to get all the properties including custom
    – Asad Refai
    Commented Aug 21, 2015 at 4:51
  • I wanted only 1 ajax call...
    – omega
    Commented Aug 21, 2015 at 4:53

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.