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

  • What about: mysite/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Office – Ransher Singh Aug 18 '15 at 20:37
  • 1
    Doesn't work with custom properties/ but the one I have works – omega Aug 18 '15 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 – Ransher Singh Aug 18 '15 at 20:45
  • do you mean accountname=@v where @v is the user id number? – omega Aug 18 '15 at 20:48
  • I tried that, it didn't work. Can you show it as an example url, maybe my syntax is off. – omega Aug 18 '15 at 20:50
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
    I love the simplicity of a single line to get the user id _spPageContextInfo.userId – Syed Nasir Abbas Aug 31 '17 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.

  • This works but still requires me to make 2 ajax calls. One to get username then one to get managed property... – omega Aug 19 '15 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

  • like I said in my first post, this doesn't work with custom managed properties... – omega Aug 19 '15 at 13:09
  • @omega : I have updated my answer with code which I used to retrieve custom properties. – Asad Refai Aug 21 '15 at 4:44
  • This looks like 2 ajax calls – omega Aug 21 '15 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 Aug 21 '15 at 4:51
  • I wanted only 1 ajax call... – omega Aug 21 '15 at 4:53

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.