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, MSDN has a decent documentation on it here:
http://msdn.microsoft.com/en-us/library/jj163800.aspx
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, MSDN has a decent documentation on it here:
http://msdn.microsoft.com/en-us/library/jj163800.aspx
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:
1234 |
|
(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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
|
2) Get Single User Profile Property:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
|
3) Get User Profile Properties of the Current User:
1234567891011121314151617181920212223242526272829303132333435363738 |
|
4) Get Properties of Multiple Users in Single Request:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
|
For this last example, Let's observe the XML which is sent to the server:
1234567891011121314151617181920 |
|
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.
Hi how can i add a user to sharepoint online
ReplyDelete