Skip to content

Instantly share code, notes, and snippets.

@teeaich
Created October 18, 2013 07:00
Show Gist options
  • Save teeaich/7037532 to your computer and use it in GitHub Desktop.
Save teeaich/7037532 to your computer and use it in GitHub Desktop.
sharepoint csom get user
var context;
var web;
var user;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
context = SP.ClientContext.get_current();
web = context.get_web();
getUserName();
});
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
// This function is executed if the above OM call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title());
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment