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> 
1
  • Have you tried out AJAX?
    – user2536
    CommentedMar 13, 2012 at 16:22

3 Answers 3

4

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

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

2
  • Thanks! I was browsing the object in debugger and must have missed that!CommentedMar 13, 2012 at 17:22
  • Nice, haven't seen that one, handy indeed :)CommentedMar 13, 2012 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('[email protected]',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.

    1
    • 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
      CommentedMar 19, 2019 at 15:06

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.