Add name of user in page

Copper Contributor

Hi, I am trying to add the users name like 

 

Hi Sylvian,

 

How can I do this ? 

Thanks for your help!

 

9 Replies

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

ganeshsanap_0-1657971134091.png

Output

ganeshsanap_1-1657971200271.png

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.

@ganeshsanap this worked well for me but how do I format the text that is produced?

@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

ganeshsanap_0-1691384657859.png


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.

Hi @ganeshsanap 

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

MVCuser_0-1707826031463.png

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

 

thank you

@MVCuser 

 

Above code does not work with the SharePoint default embed web part. You have to use the custom Modern script web part developed using SPFx.

 

You can find the custom web part code at: Script editor web part for modern pages 

 

You have to download the code and create package by following steps given at: Minimal Path to Awesome 


Please consider giving a Like if my post helped you in any way.

OH, I see thank you.
So there is not an out of the box way to achieve this. Shame.
Thank you for the details and links

@MVCuser Yes. You can only see the user name in site using SharePoint out of the box features at: 

ganeshsanap_0-1707829743637.png


Please consider giving a Like if my post helped you in any way.

Thank you.
I was looking for something more obvious like a banner saying Welcome so and so.
But thanks again for your help.
Issue with this approach is:
1. It needs to be manually added to each page, right? We like to use it on all pages.
2. User has to go to web part to modify the style, font size, etc. Prefer to do it through a richtext editor.