Search This Blog

Monday, January 9, 2012

Display Current Login User Name in Sharepoint 2010 Using Sharepoint Designer 2010


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.


<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.

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.

1 comment:

  1. Hi,

    You have a very nice blog on SharePoint.
    Can you also suggest some good book to learn programming in SharePoint.

    Please reply me on arunmanglick@gmail.com

    Regards,
    Arun Manglick
    http://arun-architect.blogspot.com/
    http://arun-ts.blogspot.com/

    ReplyDelete