1

I have a list with User og Group field. I've retrieved item from it, now I need to get user's Login. By reading User or Group field I can get only user id and account name. here's my code:

    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var item = listItemEnumerator.get_current();

        var assignedToVal = item.get_item('User Column');
        var userName = assignedToVal.get_lookupValue();
        var userId = assignedToVal.get_lookupId();

So now I have user id and name. How can I get a login?

  • So you are getting User's display name instead of user's login Name when accessing assignedToVal.get_lookupValue(); ? – Arsalan Adam Khatri Oct 2 '14 at 13:37
  • yes, exactly... – Chetvergov Oct 2 '14 at 13:39
2

You can try getting User's information from User Information List:

function getUserInfo(userId,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='ID' /><Value Type='Integer'>" + userId + "</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(userId,function(userInfo){
       console.log('User Login Name: ' + userInfo.UserName);

    },
    function(sender,args){
       console.log(args.get_message());
});

Source:

Get User via JavaScript Client Object Model

  • Thanks! I didn't know I need UserName property instead of login. Your code works just fine! – Chetvergov Oct 3 '14 at 6:33

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.