简体   繁体   中英

Sharepoint 2013 Online - Display currentUser information in page

I need to get the currentUser information, like email address and or name when a Home.aspx page is loaded in a specific area on the page like maybe the footer area. I have search and every JavaScript that I have found do not work. I need the text to display either in a Script Editor or Content Editor or an XML file.

What is wrong with the following JavaScript that I found. I have it in the WebApp Script Editor:

<script type=”text/javascript”>
ExecuteOrDelayUntilScriptLoaded(init,”sp.js”);
var currentUser;
 function init(){
  this.clientContext = new SP.ClientContext.get_current();
  this.oWeb = clientContext.get_web();
  currentUser = this.oWeb.get_currentUser();
  this.clientContext.load(currentUser);
  this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
 }

function onQuerySucceeded() {
  document.getElementById(“logUser”).innerHTML = currentUser.get_loginName(); 
 }

function onQueryFailed(sender, args) {
  alert(‘Request failed. \nError: ‘ + args.get_message() + ‘\nStackTrace: ‘ + args.get_stackTrace());
 }
< /script>
< div>Current Logged User:<label id=”logUser”></label></div>

Can anyone help me?

Most probably init method is not executed since SP.SOD.executeOrDelayUntilScriptLoaded method expects SharePoint Client Library sp.js to be loaded first. Use SP.SOD.executeFunc method instead since it supports on demand scripts.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function(){  
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var currentUser = web.get_currentUser();
  context.load(currentUser);
  context.executeQueryAsync(
  function(){
      console.log(currentUser.get_loginName()); 
  }, 
  function(sender, args){
      console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM