mylifeinaminute.com

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.

2 comments for this entry:
  1. Kirthi

    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.

  2. liquidpooled

    @Kirthi - Do you receive any JavaScript errors? Can your client run ActiveX controls?

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...