Accessing the NT user name with Javascript and ActiveX in SharePoint
by liquidpooled on Apr.08, 2008, under Internet Explorer, Microsoft, Office, Sharepoint Server
There may be times when you wish to present your users with personalized links or other types of information where their NT user id must be passed along/accessed in a form. One way to do this is with Javascript and ActiveX. I only recommend this method if you don’t have access to a master page or code behind to retrieve this information in a more traditional manner (i.e. “HttpContext.Current.User.Identity.Name” or the “SPUser” objects) AND this is being used in a controlled intranet scenario (i.e. all users are accessing your SharePoint instance with Internet Explorer).
You can retrieve the current logged in NT user name from a Content Editor web part with a combination of Javascript and ActiveX with the following:
<script type="text/javascript" language="javascript">
var objUserInfo = new ActiveXObject("WScript.network");
document.write(objUserInfo.ComputerName);
document.write(objUserInfo.UserDomain);
document.write(objUserInfo.UserName);
</script>
It is important to keep in mind that this method will not return any information from IIS or the current site being browsed, so the user name we retrieve within the ActiveX control has the potential to be different from the user name the user is currently logged in to the SharePoint instance with.
A reference to the “Wscript.Network” object can be found here.
September 2nd, 2008 on 4:13 pm
Hi,
I tried the above code to get the currently logged in user in sharepoint 2003 content editor web part, but it does not seem to be working.
September 3rd, 2008 on 10:13 pm
@Kirthi - Do you receive any JavaScript errors? Can your client run ActiveX controls?