SharePoint 2013 get current user using JavaScript

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

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

相关标签:
11条回答
  • 2020-12-13 09:59

    How about this:

    $.getJSON(_spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser")
    .done(function(data){ 
        console.log(data.Title);
    })
    .fail(function() { console.log("Failed")});
    
    0 讨论(0)
  • 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);      
    
    }
    
    0 讨论(0)
  • 2020-12-13 10:01

    I had to do it using XML, put the following in a Content Editor Web Part by adding a Content Editor Web Part, Edit the Web Part, then click the Edit Source button and paste in this:

    <input type="button" onclick="GetUserInfo()" value="Show Domain, Username and Email"/> 
    
    <script type="text/javascript">
    function GetUserInfo() {
    
    $.ajax({
        type: "GET",
        url: "https://<ENTER YOUR DOMAIN HERE>/_api/web/currentuser",
        dataType: "xml",
    
        error: function (e) {
            alert("An error occurred while processing XML file" + e.toString());
            console.log("XML reading Failed: ", e);
        },
    
        success: function (response) {
            var content = $(response).find("content");
            var spsEmail = content.find("d\\:Email").text();
            var rawLoginName = content.find("d\\:LoginName").text();
            var spsDomainUser = rawLoginName.slice(rawLoginName.indexOf('|') + 1);
            var indexOfSlash =  spsDomainUser.indexOf('\\') + 1;
            var spsDomain = spsDomainUser.slice(0, indexOfSlash - 1);
            var spsUser = spsDomainUser.slice(indexOfSlash);
            alert("Domain: "  + spsDomain + " User: " + spsUser + " Email: " + spsEmail);
        }
    });
    }
    
    </script>
    

    Check the following link to see if your data is XML or JSON:

    https://<Your_Sharepoint_Domain>/_api/web/currentuser

    In the accepted answer Kate uses this method:

    var userid= _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")
    
    0 讨论(0)
  • 2020-12-13 10:05

    You can use the SharePoint JSOM to get your current user's account information. This code (when added as the snippet in the Script Editor web part) will just pop up the user's display and account name in the browser - you'll want to add whatever else in gotAccount to get the name in the format you want.

    <script type="text/javascript" src="/_layouts/15/SP.js"></script>
    <script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script>
    <script type="text/javascript">
    
      var personProperties;
    
      SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.UserProfiles.js');
    
      function getCurrentUser() {
        var clientContext = new SP.ClientContext.get_current();
        personProperties = new SP.UserProfiles.PeopleManager(clientContext).getMyProperties();
        clientContext.load(personProperties);
        clientContext.executeQueryAsync(gotAccount, requestFailed);
      }
    
      function gotAccount(sender, args) {
        alert("Display Name: "+ personProperties.get_displayName() + 
            ", Account Name: " + personProperties.get_accountName());
      }
    
      function requestFailed(sender, args) {
        alert('Cannot get user account information: ' + args.get_message());
      }
    
    </script>
    

    See the SP.UserProfiles.PersonProperties documentation in MSDN for more info.

    0 讨论(0)
  • 2020-12-13 10:15

    Here is the code that worked for me:

    <script src="/SiteAssets/jquery.SPServices-2013.02a.js" type="text/javascript"></script>
    <script src="/SiteAssets/jquery.js" type="text/javascript"></script>
    
    <script type="text/javascript">
      var userid= _spPageContextInfo.userId;
      var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
      var requestHeaders = { "accept" : "application/json;odata=verbose" };
      $.ajax({
        url : requestUri,
        contentType : "application/json;odata=verbose",
        headers : requestHeaders,
        success : onSuccess,
        error : onError
      });
    
      function onSuccess(data, request){
        var loginName = data.d.Title;
        alert(loginName);
      }
    
      function onError(error) {
        alert("error");
      }
    </script>
    
    0 讨论(0)
提交回复
热议问题