jquery SPGetCurrentUser() 在 sharepoint 2007 中获取当前登录的用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7244679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery SPGetCurrentUser() to get the current logged in username in sharepoint 2007
提问by user346514
I would like to use jquery to get the current logged in username and pass the user information to the connectible webpart. This is what I have written in CEWP.
我想使用 jquery 获取当前登录的用户名并将用户信息传递给可连接的 webpart。这是我在 CEWP 中写的。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
var thisUserAccount = $().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});
alert(thisUserAccount);
}
);
</script>
but nothing is hapening. I dont see any output from alert also. am I doing anything wrong? Is there any better way of doing it?
但什么都没有发生。我也没有看到警报的任何输出。我做错了什么吗?有没有更好的方法来做到这一点?
Thanks in advance.
提前致谢。
回答by Virendra Kumar
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
var thisUserAccount ;
$(document).ready(function() {
thisUserAccount= $().SPServices.SPGetCurrentUser({
fieldName: "Title",
debug: false
});
</script>
回答by disp_name
Please find below code to get the user name.
请找到以下代码以获取用户名。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
var currentUserName ;
$(document).ready(function() {
currentUserName = $().SPServices.SPGetCurrentUser({
fieldName: "Title",
debug: false
});
alert(currentUserName);
</script>
You can play around with "fieldName" value to get different results for the account. Like you can give "Name" to get the account information. "Title" return the name of the logged in user. Similarly there are various other options available to fetch current user's email, picture etc.
您可以使用“fieldName”值来为帐户获得不同的结果。就像您可以给“姓名”以获取帐户信息一样。“Title”返回登录用户的名称。同样,还有各种其他选项可用于获取当前用户的电子邮件、图片等。
You can find more at SPGetCurrentUserDocumentation.
您可以在SPGetCurrentUserDocumentation 中找到更多信息。
回答by Travis Acton
Declare your "thisUserAccount" as a global
将您的“thisUserAccount”声明为全局
Define a function that assigns the thisUserAccount when called
定义一个在调用时分配 thisUserAccount 的函数
Call that function on button click or in the below example on load and use jquery deferred to assign it to the html of whatever element you want when the function run completes. In the below example, I am using a div with the id of username.
在按钮单击时调用该函数或在下面的示例中调用该函数,并在函数运行完成时使用 jquery deferred 将其分配给您想要的任何元素的 html。在下面的示例中,我使用了一个带有用户名 ID 的 div。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//declare it as global then all your function will have access to it
var thisUserAccount ;
$(document).ready(function() {
//run populate user and on finish assign the value to whatever div
$.when(populateUser()).done(function() {$('#userName').html(thisUserAccount)});
});
function populateUser(){
thisUserAccount= $().SPServices.SPGetCurrentUser({fieldName: "Name",debug: false});
}
</script>
回答by Sigar Dave
try this
尝试这个
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
var thisUserAccount ;
$(document).ready(function() {
thisUserAccount= $().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});
alert(thisUserAccount);
}
);
</script>