0

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.

san
  • 304
  • 4
  • 20
  • Web class of client object model has "SiteUserInfoList" property. It is a link to hidden list which contains all existing users on current site collection. You can try to query to get user if you do not want to use EnsureUser method. – Yevgeniy.Chernobrivets Feb 15 '14 at 09:33

1 Answers1

1

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());
});    
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Thanks for the solution. One more question.. Actually I need to update the "modified by" of a document using client object model. What if the user is not available in the site but present in AD. Is it possible to add this user to modified by field without knowing the LookupId? – san Feb 16 '14 at 08:01
  • It is recommended to import users from active directory into SharePoint (User Profile) http://blogs.technet.com/b/meacoex/archive/2013/08/04/step-by-step-active-directory-import-for-sharepoint-2013.aspx – Vadim Gremyachev Feb 16 '14 at 15:15
  • I do not have access to server. Is it fine to get the LookupId using EnsureUser and change the "modified by" field and remove the user from site? – san Feb 17 '14 at 07:00
  • I must be dense, but it is not immediately apparent to me what language this is in. JavaScript? –  Apr 15 '14 at 19:13