Home > SharePoint, SharePoint 2010, SP2010 > Getting the logged on user name using the client object model

Getting the logged on user name using the client object model

If you want to use the SharePoint 2010 client object model you can use the code below:

ExecuteOrDelayUntilScriptLoaded(getWebUserData, “sp.js“);

    var context = null;

    var web = null;

    var currentUser = null; 



        function getWebUserData() { 

        context = new SP.ClientContext.get_current();

        web = context.get_web();

        currentUser = web.get_currentUser();

        currentUser.retrieve();

        context.load(web);

        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 

             Function.createDelegate(this, this.onFailureMethod));

   }



    function onSuccessMethod(sender, args) {

        var userObject = web.get_currentUser();

        var UserTitle = userObject.get_title();

        alert(UserTitle);

        }



    function onFailureMethod(sender, args) {

        alert(‘request failed ‘);

    }





Here is a video and a simple way to test it.

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: