Tuesday, June 17, 2014

SharePoint 2013- Get/Fetch SharePoint User Details using JavaScript/Client Object Model

To fetch the current user details in the SharePoint using client object model, Add a new Content Editor on your page and insert the below code. This will display the user id, name and email of the current logged in user.

< script type="text/javascript" >
//Delay until the page loads
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.QuerySucceeded), Function.createDelegate(this,this.QueryFailed));

function QuerySucceeded() {
    document.getElementById('userLogin').innerHTML = currentUser.get_loginName();
    document.getElementById('userName').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();

function QueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());

< / script >
<!-- Display on the screen.-->
< div  >Current User Details:
    < span id="userLogin"></ span >
    < span id="userName"></ span >
    < span id="userEmail"></ span >
< /div  >

PS: White Space are added in the HTML tag to avoid the blog to hide the controls. Please clear the white space on the tags to use at your end.

Article Source
Happy Browsing!

No comments:

Post a Comment