Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is there a way to get user information via the JavaScript Client OM? Something similar to the clientContext.Web.EnsureUser(userName) method. I am aware that there is the SPService JQuery library, but if I can do this via the Client Object Model, I would prefer that. And just to be clear: I am NOT talking about the current logged in user. I want to verify the existence of a user in a manner similar to the people picker control.

Edit: Here is some example code... It assumes JQuery is loaded. Just plug it into an HTML Form webpart.

<input type="text" id="user-name">&nbsp;<input type="button" id="myButton" value="check name" />
<script type="text/javascript">
   $(document).ready($('#myButton').click(function(){
     verifyUser($('#user-name').val());
    }));

    function verifyUser(userName){
       var context = new SP.ClientContext.get_current(); 
       var web = context.get_web();
       var user = web.ensureUser(userName);
       context.load(user); 

       context.executeQueryAsync( 
           function(sender, args) {showUserData(user);}, 
           function(sender, args) {alert("Error: " + args.get_message());} 
    );
    }
    function showUserData(user){
        $('#display-user-info').html('<tr><td>Name: '+user.get_title()+'</td><td>&nbsp;Email: '+user.get_email()+'</td></tr>'
        );
        $('#user-name').val(user.get_title());
    }
</script>
<table id="display-user-info">
</table>
share|improve this question
    
Have you tried out AJAX? –  Daniel Ziga Mar 13 '12 at 16:22
add comment

3 Answers

up vote 4 down vote accepted

There is an ensureUser Method in the JS Client Object Model:

http://msdn.microsoft.com/en-us/library/ff408786.aspx

share|improve this answer
    
Thanks! I was browsing the object in debugger and must have missed that! –  Robert Kaucher Mar 13 '12 at 17:22
    
Nice, haven't seen that one, handy indeed :) –  Anders Aune Mar 13 '12 at 18:45
add comment

You could build a function using the SP.Web.siteUserInfoList.

And here is a link to an example

I'm not aware of a method like EnsureUser, there is a UserCollection with some methods for retrieving users, but looks like you have to get a site group first.

share|improve this answer
add comment

How to retrieve UserInfo by UserName from UserInfo List via CSOM

function getUserInfo(userName,Success,Error)
{
   var context = new SP.ClientContext.get_current();
   var userInfoList = context.get_web().get_siteUserInfoList();
   var query = new SP.CamlQuery();
   var viewXml = "<View> \
                    <Query> \
                       <Where> \
                           <Eq><FieldRef Name='UserName' /><Value Type='Text'>" + userName + "</Value></Eq> \
                       </Where>  \
                    </Query> \
                    <RowLimit>1</RowLimit> \
                  </View>";
   query.set_viewXml(viewXml);
   var items = userInfoList.getItems(query);
   context.load(items,'Include(Deleted,Department,EMail,FirstName,ID,IsActive,IsSiteAdmin,JobTitle,LastName,MobilePhone,Name,Notes,Office,Picture,SipAddress,UserName,WebSite,WorkPhone)');
   context.executeQueryAsync(function(){
       if(items.get_count() > 0) {
          var item = items.itemAt(0);
          Success(item.get_fieldValues());
       }
       else {
          Success(null);
       }   
     },
     Error
   );
}




//Usage
getUserInfo('username@tenant.onmicrosoft.com',function(userInfo){
       console.log('User Id: ' + userInfo.ID);
    },
    function(sender,args){
       console.log(args.get_message());
});    
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.