Displaying Current User In SharePoint Master Page

One of the requirements while I was working in SharePoint 2010 was displaying the user name in the master page.

So the following are the 2 pieces of code.

Add the following code to the body tag inside master page. Make sure the below code is outside of any tag which has the runat=”server” property set.

<script type=”text/javascript”>var loginName = “<%= Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.Name %>”;</script>

Now put the following code where ever required.

<span>Welcome, <script type=”text/javascript”>document.write(loginName);</script></span>

 

If you face the following error…check out the link https://blog.binarybits.net/?p=207

An unexpected error has occurred 

If you face the following error…check out the link https://blog.binarybits.net/?p=215

Code blocks are not allowed in this file

If you face the following error… check out the link https://blog.binarybits.net/?p=211

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).

 


Comments

  1. Thanks very much for this as I’m looking for a simple no-SharePoint Designer/Visual Studio solution to this if possible. Two questions please:

    1. Must the first code be placed in the master page or can it be placed in a code snippet webpart
    2. Do you know if this works with SP2013?

    I tried but I just get Welcome, followed by empty space.

Leave a Reply

Your email address will not be published / Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.