Question

I need to get LookUpId of a user in SharePoint 2010 using client object model. I am able to get it using EnsureUser method, but this method adds the user to the SharePoint web site if the user is not available to the site.My requirement is get the LookupId without adding the user to SharePoint Is there a alternative way to get the LookupId without using EnsureUser method? If not, after getting the LookupId using EnsureUser method, can we remove the user from the SharePoint site?

Appreciate your suggestions.

Was it helpful?

Solution

Web.SiteUserInfoList Property gets the UserInfo list of the site collection that contains the Web site.

The following code demonstrates how to get userInfo (including userId property) by userName:

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());
});    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top