dominoGuru.com
Your Development & Design Resource
Getting Windows Environment Variables with LotusScript and ActiveX
06/10/2010 01:16 PM by Chris Toohey
The other day I had a simple requirement: in a mixed Microsoft Windows environment (2000 and XP), I needed to get a handle on the active Windows User Account. Simple once you know how... but that's the point of this post. I'll show you 2 techniques to accomplish this relatively simple task: the first in LotusScript and second as an ActiveX Control for Web Browser Clients or as passthru markup in XPages.
LotusScript: Environ()
... this was the technique that I used to address my need from the other
day. I was calling Java Agents via LotusScript (a modified version of my xCopy Lotus Notes Client utility), so I used the
Environ()
Function.
From the Lotus Domino Designer 8.5 Help:
Returns information about an environment variable from the operating system.
Syntax 1
Environ[$] ( { environName | n } )
For my LotusScript Agent, I did the following:
Dim winun as String
...
winun = Environ("username")
...
Set col = db.search(|@LowerCase(shortname) =
"| + Lcase(winun) + |"|, Nothing, 0)
Special thanks to Tim P. for pointing out that a complete list of
Environment Variables (in Windows XP) can be found online. You can
alternately issue a set
command from a Command Prompt.
ActiveX Control: x.UserName
I've used this technique in the past with Internet Explorer-only shops with much success. There are, of course, some precautions that need to be in-place in your environment before this can be a completely seamless solution.
But first, the markup:
This creates a global variable in the browser (U
), that you can
access via JavaScript:
if (typeof U != 'undefined') {
var a = U;
document.getElementById('UID').value = a.toString();
...
}
The only gotcha with this technique is the ActiveX securty built into Internet Explorer... which is a good thing, don't get me wrong, but it will cause your application to malfunction if you're using ActiveX Control-based content and -- for example -- your site isn't listed in the Trusted Sites section (with ActiveX Controls auto-enabled) in the Internet Explorer Security Preferences.
Mind you, I don't recommend -- even for internal/intranet applications -- that you allow any ActiveX Controls to run without check (nastiness will ensue!). I instead tend to write a hidden warning message on-screen instructing the user to reload and accept/launch the ActiveX Control:
if (typeof U != 'undefined') {
var a = U;
document.getElementById('UID').value = a.toString();
...
} else {
document.getElementById('ac_warning').className = "";
}
The ac_warning HTML Element (often a DIV with a className set to hidden with cooperating CSS) contains something like this (a screencap from one of my production customer applications):
Conclusion
I'm playing around with a simple SSJS approach to querying Windows Environment Variables so we can use it in Lotus Notes Client and Web Browser Client applications now, so expect to see that article as soon as that's finished. Until then, here are two techniques that you can use today.
... and what might you use them for?! Well, let's say a customer wants to export NotesData from your application and bring it into Microsoft Excel. Using this technique, you could easily leverage the user's Windows Temp directory (or their User Profile Temp Directory) as the default target for your export.
Hopefully those of you still writing LotusScript (like me...) or having to
write Web Browser apps for IE shops (like me...) will find this useful. If
you'd like to see more stuff like this (categorized -- at least the
Environ()
Function -- under stuff Chris should have known by
now...) let me know via feedback and I'll see what else I can come up
with.