Does SharePoint 2010 store the current user's accountname somewhere in a globally accessible JS object?
Every solution I can find involves some variation of an ajax web service call and this seems like an extremely heavy solution to access what should be a trivial piece of information.
Things I can access easily:
__spUserId
$('#zz17_Menu').text()
)But neither of these is the accountname.
Things I would rather not do:
For anyone else who stumbles across this, a year later I figured out how.
To your master page register SPSWC (if you're using Randy Drisgill's starter master it will already be there).
<%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Then, before you want to access a property (eg: UserName) pull in the data
<SPSWC:ProfilePropertyLoader runat="server"/>
Next, access one of the properties, I would recommend using TitleMode to embed it in a <script>
element.
<script>
var username = '<SPSWC:ProfilePropertyValue PropertyName="UserName" TitleMode="true" runat="server"/>';
</script>
We now use it as shown in this PasteBin demo to populate a global with a few properties like first name, last name, user name, profile picture, status.
You really need to make a web service call to do it reliably. Thankfully, SPServices makes it quick and painless to do so.
http://spservices.codeplex.com/wikipage?title=$%28%29.SPServices.SPGetCurrentUser
$().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With