2

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>
  • Have you tried out AJAX? – user2536 Mar 13 '12 at 16:22
4

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

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

  • 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
4

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());
});    
1

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.

  • Please make sure you avoid linking to answers. That example link is broken. Should summarize the contents or provide a brief example here so this becomes an authoritative source. – vapcguy Mar 19 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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