1

I want to use SharePoint 2013 rest api to get user data but using the search API.

I realize I need to first find the user record from the people content type. The only information I have by default is _spPageContextInfo.userId which gives me a number. The problem is I don't know what is the name of this column in the managed properties or something. I don't know if I need to create a new managed property to map that. Is there a way I can uniquely identify the current user in Search REST api?

Thanks

1 Answer 1

0

A bit late to the party, but came across this issue myself, so i'll add my answer.

By example lets say you want to find items with the myUserField field. The myUserField field is mapped to the managed property myUserFieldOWSUSER (if you have your own fields you can see how it is mapped in the search schema).

Once you know the managed property the search results webpart will you query like this:

myUserField:{User}

...which is very convenient.

In the case of REST API you cannot use that {User} apperently. Therefore we first need to get current user's accountname by using the PeopleManager.

$.ajax({
        url: "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/UserProfileProperties",
        method: 'GET',
        headers: {
            'accept': 'application/json;odata=verbose',
        }
    });

Then we can use the email address (WorkEmail or firstname-lastname) to query a search person field:

/_api/search/query?Querytext='dvwUserOWSUSER:[email protected]'

Now because I didn't like to have to do 2 calls (the queries are done for many users) I decided to add an additional site column to my original item which is 'userid' which is of type number. I have some javascript setting the field with the _spPageContextInfo.userId

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.