.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Resource Bookmark and Share   

Get the Current Logged in User Name in Sharepoint Using JSON

Posted By:Sagar Pardeshi       Posted Date: April 30, 2014    Points: 40    Category:    URL: http://www.dotnetspark.com  

Get the Current Logged in User Name in Sharepoint Using JSON

<script src="/Style Library/scripts/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () { 


  SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.UserProfiles.js');

  function getCurrentUser() {
    var clientContext = new SP.ClientContext.get_current();
    personProperties = new SP.UserProfiles.PeopleManager(clientContext).getMyProperties();
    clientContext.executeQueryAsync(gotAccount, requestFailed);

  function gotAccount(sender, args) {
       currentUserAccount = personProperties.get_accountName();

  function requestFailed(sender, args) {
    //alert('Cannot get user account information: ' + args.get_message());


var currentUser;
// Ensure that the SP.js is loaded
if (SP.ClientContext != null) {
  SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');
else {
  SP.SOD.executeFunc('sp.js', null, getCurrentUser);
function getCurrentUser() {
   context = new SP.ClientContext.get_current();
    web = context.get_web();
  currentUser = web.get_currentUser();
  context.executeQueryAsync(onSuccessMethod, onRequestFail);

function onSuccessMethod(sender, args) {
  var account = currentUser.get_loginName();
  var title = currentUser.get_title();
  currentUserAccount = account.substring(account.indexOf("|") + 1);

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
 // alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend