Home > Work > Using jQuery to find the current logged in user #in #SP2010

Using jQuery to find the current logged in user #in #SP2010

Problem:

In MOSS 2007, we had methods to get the current logged in user by accessing some of the properties found in the Welcome Menu.

Although this is still the case in SharePoint Server 2010, there are some fundamental changes to the code and the way the Welcome Menu is rendered in SPS 2010 that means we have to do something slightly different to get the same result.

I have recently had a requirement to achieve this in my current project and thought I would share the basics with you.

Essentially we are still retrieving the information from an HTML element property in the Welcome Menu, but the menu itself is rendered in AJAX and therefore is not necessarily available as soon as the page is rendered and the DOM document ready state is set.

As a result of this, running JavaScript / jQuery on the page at either:

  • $(document).ready
  • _spBodyOnLoadFunctionNames

will not return the required result, since the menu elements we need to interrogate have not been rendered yet.

Solution:

  • Set a timer to keep running a particular function to wait until the menu is rendered
  • Stop the timer
  • Interrogate the DOM as required using jQuery

Code Example:

1 <script type='text/javascript'> 2 3 var timerHandle = ""; 4 var currentWindowsAccount = ""; 5 _spBodyOnLoadFunctionNames.push("runCustom"); 6 7 function runCustom() { 8 timerHandle = setTimeout(getCurrentLoggedInUser, 10); 9 } 10 11 function getCurrentLoggedInUser() { 12 //Function requires jQuery 13 14 /* Retrieve the current logged in user from the My Profile option in ** 15 ** the Welcome Menu (id=ID_MySiteLinksMenu) ** 16 ** This needs a timeout to wait for the menu to be rendered via AJAX */ 17 18 //Timers are expensive on client resources, clear at all opportunities 19 clearTimeout(timerHandle); 20 21 var welcomeMenuItems = $('#ID_MySiteLinksMenu'); //ie:menu with id='ID_MySiteLinksMenu' 22 23 if (welcomeMenuItems.length > 0) { 24 var onmenuclickValue = welcomeMenuItems.attr("onmenuclick"); 25 var onmenuclicksplit = onmenuclickValue.split("="); 26 var loggedInUserString = onmenuclicksplit[1]; 27 var loggedInUserStringSplit = loggedInUserString.split("'"); 28 29 currentWindowsAccount = loggedInUserStringSplit[0]; 30 alert(currentWindowsAccount); 31 } 32 else { 33 //My Profile link has not been rendered yet, try again. 34 timerHandle = setTimeout(getCurrentLoggedInUser, 10); 35 } 36 } 37 38 </script>

Advertisements
Categories: Work Tags: , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: