SharePoint 2013 has added a variety of functionality to the Client API. One of them is the ability to fetch User Profile data. Now you can directly query the user profiles and get the required data from the client site. This can be really useful if you are building apps.
If you want to use the REST API to work with user profiles, please see one of my other posts: http://www.vrdmn.com/2013/07/sharepoint-2013-get-userprofile.html
In this post, I will show you how to work with User Profiles using the JavaScript Client Object Model
Before we get started with the code, lets make couple of things sure. First, you will need a reference to the following JavaScript files in your page:
(jQuery is not required but I have added it because we will need it for the $.ajax function when doing REST queries)
Second thing, you will need the domain\username of the user you want to get the user profile data for. This can be easy to get if you are in an On-Prem environment. But if you are working with SharePoint Online, this can be quite tricky. Your username might not always be in the domain\username format. It is stored in the LoginName property if you are querying the Site User Information List:
https://yoursite.sharepoint.com/sites/pubsite/_api/Web/GetUserById(17)
and it is stored in the AccountName property if your querying the User Profile Service:
https://yoursite.sharepoint.com/sites/pubsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties
In SharePoint Online, it will most probably be in the following format:
Quick Note: If you are using the REST API, you will need to encode the username before you use it in your call. The encodeURIComponent( ) function can be helpful here.
Enough talking. Lets jump into some code right away:
For this last example, Let's observe the XML which is sent to the server:
With this, we can confirm that only one call was made to the server to retrieve the properties of multiple users. This way, you can even retrieve multiple properties of multiple users in one call.
If you want to use the REST API to work with user profiles, please see one of my other posts: http://www.vrdmn.com/2013/07/sharepoint-2013-get-userprofile.html
In this post, I will show you how to work with User Profiles using the JavaScript Client Object Model
Before we get started with the code, lets make couple of things sure. First, you will need a reference to the following JavaScript files in your page:
(jQuery is not required but I have added it because we will need it for the $.ajax function when doing REST queries)
Second thing, you will need the domain\username of the user you want to get the user profile data for. This can be easy to get if you are in an On-Prem environment. But if you are working with SharePoint Online, this can be quite tricky. Your username might not always be in the domain\username format. It is stored in the LoginName property if you are querying the Site User Information List:
https://yoursite.sharepoint.com/sites/pubsite/_api/Web/GetUserById(17)
and it is stored in the AccountName property if your querying the User Profile Service:
https://yoursite.sharepoint.com/sites/pubsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties
In SharePoint Online, it will most probably be in the following format:
i:0#.f|membership|vardhaman@yoursite.onmicrosoft.com
Quick Note: If you are using the REST API, you will need to encode the username before you use it in your call. The encodeURIComponent( ) function can be helpful here.
Enough talking. Lets jump into some code right away:
1) Get Multiple User Profile Properties:
2) Get Single User Profile Property:
3) Get User Profile Properties of the Current User:
4) Get Properties of Multiple Users in Single Request:
For this last example, Let's observe the XML which is sent to the server:
With this, we can confirm that only one call was made to the server to retrieve the properties of multiple users. This way, you can even retrieve multiple properties of multiple users in one call.