This article explains how to get the user properties in SharePoint Online.
Now you can directly query the user profiles and get the required data from the client side.
In SharePoint Online we can only deploy the sandboxed solutions. There are some restrictions in using Microsoft.Office.Server.UserProfiles in SharePoint sandboxed solutions. If you want to get properties from user profiles then you can use the script shown below.
Step 1: In the SharePoint sandboxed web part (.ascx page) add the following references as shown below.
(jQuery is not required but I have added it because we will need it for the $.ajax function when doing REST queries.)
In SharePoint Online we can only deploy the sandboxed solutions. There are some restrictions in using Microsoft.Office.Server.UserProfiles in SharePoint sandboxed solutions. If you want to get properties from user profiles then you can use the script shown below.
Step 1: In the SharePoint sandboxed web part (.ascx page) add the following references as shown below.
(jQuery is not required but I have added it because we will need it for the $.ajax function when doing REST queries.)
- <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
- <script src="/_layouts/15/SP.Runtime.js"></script>
- <script src="/_layouts/15/SP.js"></script>
- <script src="/_layouts/15/SP.UserProfiles.js"></script>
Here is the script segment to get the current user profile properties. Add the following script in your web part “.ascx” page:
- <script type="text/javascript">
- (function ($) {
- //debugger;
- $(document).ready(function () {
- // ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
- SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
- });
- var userProfileProperties;
- function loadUserData()
- {
- //debugger;
- //Get Current Context
- var clientContext = new SP.ClientContext.get_current();
- //Get Instance of People Manager Class
- var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
- userProfileProperties = peopleManager.getMyProperties();
- clientContext.load(userProfileProperties);
- clientContext.executeQueryAsync(onSuccessful, onFailure);
- }
- function onSuccessful (sender, args) {
- //debugger;
- //Get default properties
- var username = userProfileProperties.get_displayName();
- var desigination = userProfileProperties.get_title();
- var pictureURL = userProfileProperties.get_pictureUrl();
- //Get custom properties
- var employeeID = userProfileProperties.get_userProfileProperties().EmployeeID;
- // alert(username + "/" + pictureURL + "/" + employeeID + "/" + desigination);
- // lblEmployeeDesignationValue, lblEmployeeIdValue – lable id
- document.getElementById('<%= lblEmployeeDesignationValue.ClientID %>').innerHTML = desigination;
- document.getElementById('<%= lblEmployeeIdValue.ClientID %>').innerHTML = employeeID;
- if (pictureURL != null || pictureURL == "" ) {
- document.getElementById('<%= imgUser.ClientID %>').src = pictureURL;
- }
- else {
- document.getElementById('<%= imgUser.ClientID %>').src =
- “/_catalogs/masterpage/MetroOnlineLMSStyles/images/UserImage.jpg"
- }
- }
- function onFailure(sender, args)
- {
- alert("Error: " + args.get_message());
- }
- </script>
That's all.
Get Properties of Multiple Users in Single Request
Here is the code segment to get the multiple user profile properties in a single request. Add the following script in your web part “ASCX” page.
We can use the following script for SharePoint on-premises and online (o365) also.
Get Properties of Multiple Users in Single Request
Here is the code segment to get the multiple user profile properties in a single request. Add the following script in your web part “ASCX” page.
We can use the following script for SharePoint on-premises and online (o365) also.
- <script type="text/JavaScript">
- (function($)
- {
- $(document).ready(function()
- {
- // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
- SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
- });
- var userProfileProperties = [];
- //Array containing domain\usernames of multiple users. You can get the usernames any way you want.
- var targerUsers = ["i:0#.f|membership|user@yoursite.onmicrosoft.com","i:0#.f|membership|user1@yoursite.onmicrosoft.com"];
- //If you are on On-Premise:
- //var targerUsers = ["domain\\username","domain\\demouser1"];
- function loadUserData(){
- //Get Current Context
- var clientContext = new SP.ClientContext.get_current();
- //Get Instance of People Manager Class
- var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
- //Property to fetch from the User Profile
- var propertyName = "PreferredName";
- for(var i=0;i<targerUsers.length;i++){
- //Create new instance of UserProfileProperty
- userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);
- }
- //Execute the Query. (No load method necessary)
- clientContext.executeQueryAsync(onSuccessful, onFailure);
- }
- //Success method
- function onSuccessful () {
- var message = "";
- for(var i=0;i<userProfileProperties.length;i++){
- message += "\" Name\" property is " + userProfileProperties[i].get_value();
- }
- alert(message);
- }
- //Failure method
- function onFailure (sender, args) {
- alert("Error: " + args.get_message());
- }
- })(jQuery);
- </script>
That's all. The code above gets the multiple user properties in a single request.
No comments :
Post a Comment