[英]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). 我需要创建一个简单的静态Web部件 - 唯一的动态部分是当前用户登录名(需要包含在链接中的URL参数中)。
I'd rather just use a content editor web part instead of a custom web part that I'll need to deploy. 我宁愿只使用内容编辑器Web部件而不是我需要部署的自定义Web部件。 In fact, I already did that using JavaScript and ActiveX (
WScript.Shell
- full code below) 事实上,我已经使用JavaScript和ActiveX(
WScript.Shell
- 下面的完整代码)做到了这一点
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.
我不喜欢ActiveX方法的是它需要更自由的IE配置,即使我在安全设置中启用与ActiveX相关的所有内容,仍然需要点击提示。 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? 我能想到的一种方法是使用jQuery(或者只是JavaScript)从右上角抓取用户名,但有更清洁的东西吗? 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: 在您的内容编辑器Web部件中包含此Web部件令牌 :
_LogonUser_
It outputs the same value as Request.ServerVariables("LOGON_USER") 它输出与Request.ServerVariables(“LOGON_USER”)相同的值
I've done this before including in a hidden span and plain old document.getElementById to get the innertext. 我之前已经完成了这个,包括隐藏的span和普通的旧document.getElementById来获取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. 我检查了页面的来源,用户名不在那里,所以不能被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.
浏览器似乎没有随请求一起发送它(就像它与您的本地IP和其他客户端相关的信息一样),因此无法从头文件中获取。 The only other approach I can conceive is calling a web service from the client-side that would be an overkill for my scenario.
我能想到的唯一另一种方法是从客户端调用Web服务,这对我的方案来说太过分了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.