none
Sharepoint RRS feed

  • Question

  • Hi all,
    For a client need I need to develop a component to display the current user in the master page, so when i am on the site for example https://Tenant.sharepoint.com/sites/SiteName/ I have this error : 
    Request failed Unexpected response data from server. null
    In console :Failed to load resource: net::ERR_NAME_NOT_RESOLVED      https://tenant.sharepoint/_api/contextinfo

    Knowing that i have defined the welcome page and the redirection does not work but when I put URL manually its walking no problem. 

    My script :


    function GetLoggedInUserName(){
    var context = new SP.ClientContext.get_current();
    this.website = context.get_web();
    this.currentUser = website.get_currentUser();
    context.load(currentUser);
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
     
    function onQuerySucceeded(sender, args){
    document.getElementById('LoginExtranet').innerHTML = currentUser.get_title();
    }
     
    function onQueryFailed(sender, args){
    alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
    }
    ExecuteOrDelayUntilScriptLoaded(GetLoggedInUserName,'sp.js','SP.ClientContext');   



    Tuesday, February 28, 2017 11:42 AM

All replies

  • Hi ,
    There is welcome menu in the master page which displays Current user name.
    You can use it as per your requirement and also change the look and feel of that particular component.
    Tuesday, February 28, 2017 3:51 PM
  • Hi Shachchin,

    I am on SharePoint online with a publishing site and I have to remove all the suite bar, I  have already tried your proposal but it is difficult to manage on sharepoint online

    Thank you.

    Tuesday, February 28, 2017 4:35 PM
  • Use this code and let me know 

    <script type="text/javascript">
    
    var loginName = "";
    var userid = _spPageContextInfo.userId;
    GetCurrentUser();
    
    function GetCurrentUser() {
    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("Hello " + loginName);
    
        // to set the "hello username" into the page
        document.getElementById("id").innerHTML = "Hello " + loginName;
        }
    
    function onError(error) {
      alert(error);
      }
    
    </script>

    OR

    <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>

    or 

     function getCurrentUser() 
        {  
            console.log('Made it');
            var ctx = SP.ClientContext.get_current();
            var web = ctx.get_web();
            var currentUserObj = web.get_currentUser();
            ctx.load(currentUserObj);
            ctx.executeQueryAsync(
                function() 
                {
                    var displayName = currentUserObj.get_title();           
                    $('#name').val(displayName);
                }, 
                    function(sender, args) 
                    {
                         console.log("An error occured: " + args.get_message());
                    }); //end get currentUser display
        }

    Tuesday, February 28, 2017 5:54 PM