Display Current Login username in sharepoint control

Cp_raj
Posted by in SharePoint category on for Intermediate level | Points: 250 | Views : 33591 red flag

In this article, we are going to learn how to display current login username in sharepoint using Javascript.

Introduction


Sometime we need to display the current login username in people & group sharepoint control. In this article, we shall learn how to do that.

Source Code

We got a requirement to display the current login username in sharepoint control.We have only access to sharepoint designer. We dont have access to add serverside code.  So we decide to user "Welcome username" message. We used javascript to display the username. Please check the following code.

Open sharepoint designer, copy and paste the code in appropriate aspx sharepoint page


<script type="text/javascript" language='javascript'>
function getLoginName()
 {
 var tags = document.getElementsByTagName('span');
 var loginName = null;
     for (var i=0; i < tags.length; i++)
     {
    if(tags[i].title =="Open Menu")
       {
          var childTag = tags[i].getElementsByTagName('DIV');
             var innerChild = childTag[0].getElementsByTagName('a');
          var tloginName = innerChild[0].innerText;
          var iLength = tloginName.length;
    if(tloginName.indexOf("Welcome") > -1)
    {
     loginName = tloginName.substring(8,iLength);
     break;
    }
       }
  }
  return loginName;
}


 

//To display in people & group control

function setPickerInputElement(pickerNo)
{
  var result = "";
  var j = 0;
  var divs = document.getElementsByTagName("DIV");
  for (var i=0; i <30; i++)
   {
       if (divs[i].id.indexOf("UserField_upLevelDiv") > 0)
       {
           j++;
           if (j == pickerNo)
           {
              result = divs[i];
              result.innerText = getLoginName(); 
              break;
           }
        }
    }

 }
</script>


 

setPickerInputElement(1);

call this function, and pass the people & group No.if there is only one people & control use "1", if you have more than 1, give on which control the current username need to display.

Screen

Screen1:Paste the code as shown.


Screen 2:



With this approach, instead of people or group control, you can display the user name in textbox if required.... 

Thanks for reading, keep learning and sharing !

Page copy protected against web site content infringement by Copyscape

About the Author

Cp_raj
Full Name: Rajkumar Pauldurai
Member Level: Starter
Member Status: Member
Member Since: 5/26/2009 6:29:52 AM
Country:

http://www.dotnetfunda.com
I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)