SharePoint 2013: Working with User Profiles & JavaScript CSOM
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
1. Get User Properties
<Script type="text/javascript">
(function($){
$(document).ready(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});
var userProfileProperties = [];
function loadUserData(){
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
var profilePropertyNames = ["PreferredName","PictureURL"];
var targetUser = "domain\\username";
var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
//Execute the Query.
clientContext.load(userProfilePropertiesForUser);
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
var userName= "Name : " + userProfileProperties[0];
document.getElementById("user").innerText = userName ;
}
function onFail(sender, args) {
alert("Error: " + args.get_message());
}
})(jQuery);
</script>
<div id="user"/>
2. Get particular user property
<Script type="text/javascript">
(function($){
$(document).ready(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});
var userProfileProperty;
function loadUserData(){
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
var propertyName = "PreferredName";
var targetUser = "domain\\username";
userProfileProperty = peopleManager.getUserProfilePropertyFor(targetUser, propertyName)
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
var pName = "Preferred Name: " + userProfileProperty.get_value();
document.getElementById("user").innerText = pName;
}
function onFail(sender, args) {
alert("Error: " + args.get_message());
}
})(jQuery);
</script>
<div id="user"/>
3. Current User
<script type="text/javascript">
(function($){
$(document).ready(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});
var userProfileProperties;
function loadUserData(){
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties();
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
alert(userProfileProperties.get_displayName());
}
function onFail(sender, args) {
alert("Error: " + args.get_message());
}
})(jQuery);
</script>
More Info : http://www.vrdmn.com/2013/02/sharepoint-2013-working-with-user.html
Comments
Post a Comment