SharePoint 2013 get current user using JavaScript

前端 未结 11 2457
挽巷
挽巷 2020-12-13 09:32

How to get current user name using JavaScript in Script Editor web part?

11条回答
  •  独厮守ぢ
    2020-12-13 10:01

    U can use javascript to achive that like this:

    function loadConstants() {
    
        this.clientContext = new SP.ClientContext.get_current();
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();  
        this.clientContext.load(currentUser);
          completefunc:this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
    
    
    }
    
    //U must set a timeout to recivie the exactly user u want:
    
    function onQuerySucceeded(sender, args) {
    
        window.setTimeout("ttt();",1000);
    }
    function onQueryFailed(sender, args) {
    
        console.log(args.get_message());
    }
    
    //By using a proper timeout, u can get current user :
    
    function ttt(){ 
    
        var clientContext = new SP.ClientContext.get_current();
        var groupCollection = clientContext.get_web().get_siteGroups();
        visitorsGroup = groupCollection.getByName('OLAP Portal Members');
    
        t=this.currentUser .get_loginName().toLowerCase();
    
        console.log ('this.currentUser .get_loginName() : '+ t);      
    
    }
    

提交回复
热议问题