问题
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.
<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.
回答1:
<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>
回答2:
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>
回答3:
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.
You can find more at SPGetCurrentUserDocumentation.
回答4:
Declare your "thisUserAccount" as a global
Define a function that assigns the thisUserAccount when called
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.
<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>
来源:https://stackoverflow.com/questions/7244679/jquery-spgetcurrentuser-to-get-the-current-logged-in-username-in-sharepoint-20