简体   繁体   中英

Get current username on the client side on SharePoint 2007

I need to create a simple static web part - the only dynamic part is the current user login name (needs to be included in a URL parameter in a link).

I'd rather just use a content editor web part instead of a custom web part that I'll need to deploy. In fact, I already did that using JavaScript and ActiveX ( WScript.Shell - full code below)

Is there a better, more native way to do it? What I don't like about the ActiveX approach is that it requires more liberal configuration of IE, and even when I enable everything ActiveX-related in the security settings there is still a prompt that needs to be clicked. Cross-browser support is not a major issue, it's intranet - but will be a nice extra, too.

One way I can think of is to scrape the username from the top-right hand corner with jQuery (or just JavaScript), but is there something even cleaner? Or maybe someone has already done the scraping and can share the code to save me some time ;-)

Here's my current solution:

<script language="javascript">
    function GetUserName()
    {
        var wshell = new ActiveXObject("WScript.Shell");
        var username = wshell.ExpandEnvironmentStrings("%USERNAME%");

        var link = document.getElementById('linkId');
        link.href = link.href + username.toUpperCase();
    }
</script>
<P align=center>
    <a id="linkId" onclick="GetUserName();" href="<my_target_URL>?UserID=">open username-based URL</a>
</P>

Include this Web part token in your content editor web part:

_LogonUser_

It outputs the same value as Request.ServerVariables("LOGON_USER")

I've done this before including in a hidden span and plain old document.getElementById to get the innertext.

My conclusion is that there's no better way to do that (which is also sufficiently easy).

I checked the source of the page and the username is not anywhere there, so can't be scraped by JavaScript/jQuery. The browser doesn't seem to be sending it with the request (as it does with your local IP and other client-related information), so can't be obtained from headers either. The only other approach I can conceive is calling a web service from the client-side that would be an overkill for my scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM