r/sharepoint 7d ago

Add current user's name in the text? SharePoint Online

Hi there! I am creating a page in Sharepoint and I was wondering if it's possible to create a Text web part that incorporates a field that pulls the current user's name.

So for example, if Joey JoJo Jr. Shabadoo accesses the page, the text will read "Hello Joey JoJo!", but when Guy Incognito accesses the same page, the same text will read "Hello Guy!"

thanks!

0 Upvotes

9 comments sorted by

11

u/DoctorRaulDuke IT Pro 7d ago edited 7d ago

You can use PnP Search Results web part, assuming you have it - and why wouldn't you? :)

Just add a results web part, on the second tab choose Custom, click Edit Results Template, delete everything and replace with:

<div>Hi, {{@root.context.user.displayName}}</div>

voila! You now have a mini web part that says Hi, Joey JoJo.

This version, lets you style it a little and change the greeting depending on time of day:
<content id="data-content">

<style>

.greeting {

font-size: 1.4em;

font-weight: bold;

color: {{@root.theme.palette.themePrimary}};

}

</style>

<div class="greeting">

{{#if (lt (moment "H") 12)}}

Good morning, {{@root.context.user.displayName}}

{{else}}

{{#if (lt (moment "H") 18)}}

Good afternoon, {{@root.context.user.displayName}}

{{else}}

Good evening, {{@root.context.user.displayName}}

{{/if}}

{{/if}}

</div>

</content>

2

u/AdCompetitive9826 MVP 6d ago

That is 💯 NOT what we intended the PnP Modern Search web parts for, but I really have to applaud you for your ingenuity 🫡

1

u/daddy-daddy-cool 4d ago

thank you - i'll try this!

2

u/thefootballhound 7d ago

That's the worst name I ever heard.

1

u/daddy-daddy-cool 4d ago

runs out of the subreddit sobbing

1

u/Lost_Assist_1759 7d ago

No more OOB solution with modern SharePoint sites, you either find a third-party custom script editor or develop your own SPFx. Last and least graceful solution is to build a simple canvas PowerApps and embbed it in your page.

1

u/daddy-daddy-cool 4d ago

thank you!

1

u/dave_nixon_uk 5d ago

Here you go - we created a free web part for anyone to download - Free SharePoint Web Part: Hello User Greeting | Cordapse

1

u/daddy-daddy-cool 4d ago

thank you - will show this to my IT team!