jquery SPGetCurrentUser() to get the current logged in username in sharepoint 2007

眉间皱痕 提交于 2020-01-15 07:43:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!