Forum Discussion

tifanny034's avatar
tifanny034
Copper Contributor
Jul 15, 2022

Add name of user in page

Hi, I am trying to add the users name like 

 

Hi Sylvian,

 

How can I do this ? 

Thanks for your help!

 

  • tifanny034 Do you want to add name of user on all SharePoint pages or single page?

     

    Unfortunately, there is no SharePoint default web part available to show the current logged in user's name OR web part which supports using custom JavaScript.

     

    I have previously achieved this using Script editor web part for modern pages developed by Mikael Svenson using SPFx. Here's the configuration of web part: 

    Output

    Code used

     

    <p id="pWelcomeMsg"></p>
    
    <script type="text/javascript">
    	document.getElementById("pWelcomeMsg").innerHTML = "Hey " + _spPageContextInfo.userDisplayName + ", welcome to our site";
    </script>

     


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

    • MVCuser's avatar
      MVCuser
      Brass Contributor

      Hi ganeshsanap 

      Tried a few times with the code you provided but keep getting this error message

      also, i do not see the Modern Script editor part anywhere, is that an old feature?

       

      thank you

      • ganeshsanap's avatar
        ganeshsanap
        MVP

        nmjayhawk You can use CSS styles.

         

        For example: You can add below styling block for showing the generated text in red color: 

         

        <style>
            p#pWelcomeMsg { color: red }
        </style>

        Add such style block before your HTML and JavaScript code given in above response.

         

        Output


        Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Resources