Sunday, April 02, 2006
« Cross-Browser AJAX for SharePoint Lists ... | Main | Flash Slideshow Web Part Version 1.2 rel... »

Sometimes when working in client-side javascript you'd like to know your Windows username for instance to add to the root Outlook Web Access URL for say a contacts search i.e. http://flexnetowa/exchange/colinb/contacts/?cmd=search 

Now the stock answer whenever this comes up in the SharePoint newsgroups is: it can't be done, use server side code to render it out.

Thats fair enough and easy to do, I've created a WebPart that stores a users details as a JavaScript object so it can be referenced in code. But what if I don't want to install yet another server-side Web Part.

In the interests of providing code you won't get anywhere else here's the tip.

Now its true the SharePoint Web Services provide no method to tell you who you are (side-note: I suspect you may be able to do this by creating a CAML query against the userinfo table but I've not managed to get that working) but you can get your username by using the FrontPage RPC and calling author.dll, this is how Word displays who you are on its TaskPane when you open a Word document in a document library.

When you call the author.dll ISAPI DLL with the 'open service' method name it returns a host of information about the server and the services it provides.

I'm using the XmlHttpRequest object again which despite the name can also be used to retrieve a text document not just an XML one.

heres the returned information

<html><head><title>vermeer RPC packet</title></head>
<body>
<p>method=open service:6.0.2.6356
<p>service=
<ul>
<li>service_name=
<li>meta_info=
<ul>
<li>vti_defaultlanguage
<li>SW|en-us
<li>vti_usernames
<li>VR|
<li>vti_servercharsets
<li>VX|windows-1257 big5 windows-1252 windows-874 utf-8 windows-1251 windows-1256 euc-kr gb2312 windows-1253 windows-1258 koi8-r iso-8859-1 gb18030 iso-2022-jp ks_c_5601-1987 windows-1250 windows-1255 us-ascii euc-jp unicode unicodeFFFE windows-1254 iso-8859-2 iso-8859-15 shift_jis
<li>vti_scnoprompt
<li>IX|1
<li>vti_toolpaneurl
<li>SX|http://sharepoint.flexnet.ds/_layouts/1033/toolpane.aspx
<li>vti_assemblyversion
<li>SX|Microsoft.SharePoint, Version&#61;11.0.0.0, Culture&#61;neutral, PublicKeyToken&#61;71e9bce111e9429c
<li>vti_webtemplate
<li>IR|1
<li>vti_hasonetlayoutfiles
<li>BR|true
<li>vti_navbuttonnextlabel
<li>SR|Next
<li>vti_casesensitiveurls
<li>IX|0
<li>vti_htmlextensions
<li>SX|.html.htm.shtml.shtm.stm.htt.htx.asp.aspx.alx.asa.hta.htc.jsp.cfm.odc.dwt.php.phtml.php2.php3.php4.
<li>vti_approvallevels
<li>VR|Approved Denied Pending&#92; Review
<li>vti_themedefault
<li>SR|none
<li>vti_welcomenames
<li>VX|default.htm default.aspx
<li>vti_servertz
<li>SX|+0100
<li>vti_adminurl
<li>SX|http://sharepoint.flexnet.ds/_layouts/1033/settings.aspx
<li>vti_showhiddenpages
<li>IW|1
<li>vti_categories
<li>VR|Business Competition Expense&#92; Report Goals/Objectives Ideas In&#92; Process Miscellaneous Planning Schedule Travel VIP Waiting
<li>vti_featurelist
<li>VX|vti_RulesScript vti_ServerIndexServer vti_TimedDocEvents vti_ServiceMarkUrlDirExec vti_DocSaveToDB vti_ServiceMarkUrlDirBrowse vti_ACAll vti_ServerODBC vti_ServerASP vti_ServiceMarkUrlDirScript
<li>vti_hasfulltextsearch
<li>IX|1
<li>vti_defaultcharset
<li>SR|windows-1252
<li>vti_navbuttonuplabel
<li>SR|Up
<li>vti_httpdversion
<li>SX|Microsoft-IIS/6.0
<li>vti_serverlanguages
<li>VX|en-us
<li>vti_sourcecontrolproject
<li>SX|&#60;STS-based Locking&#62;
<li>vti_doclibwebviewenabled
<li>IX|1
<li>vti_extenderversion
<li>SR|6.0.2.6361
<li>vti_ignorekeyboard
<li>IR|0
<li>vti_navbuttonprevlabel
<li>SR|Back
<li>vti_longfilenames
<li>IX|1
<li>vti_sourcecontrolsystem
<li>SX|lw
<li>vti_username
<li>SX|FLEXNET&#92;colinb
<li>vti_navbuttonhomelabel
<li>SR|Home
<li>vti_sourcecontrolcookie
<li>SX|fp_internal
<li>vti_sitecollectionurl
<li>SX|/
<li>vti_language
<li>IR|1033
</ul>
</ul>
</body>
</html>

 

Notice my windows username is listed against the vti_username item.

Using the XmlHttpRequest object to call author.dll synchronously (we're going to wait for the call to return)

xmlrequest = "method=open service:6.0.2.6356&service_name=";

if (!xmlhttp) xmlhttp=GetHTTPObject();

xmlhttp.open("POST", "/_vti_bin/_vti_aut/author.dll ",false);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("X-Vermeer-Content-Type","application/x-www-form-urlencoded");

xmlhttp.send(xmlrequest);

The responseText property on xmlhttp contains the returned document.

The hardest bit is parsing the result to extract the username.

Once that is done I show the username in a DIV element.

So the next time someone asks this question we'll be able to give them some options.

ClientSideUsername.dwp (2.92 KB)