0

I've some peoplepicker in sharepoint page and i need to get the user value of each one. I've used this page code to create the pickers. The script in the page retrieve the user id of a Picker control and put it into a div section using this function:

function getUserId(loginName) {
 var context = new SP.ClientContext.get_current();
 this.user = context.get_web().ensureUser(loginName);
 context.load(this.user);
 context.executeQueryAsync(
 Function.createDelegate(null, ensureUserSuccess),
 Function.createDelegate(null, onFail));

 }
 function ensureUserSuccess() {

    $('#userTemp').html(this.user.get_id());
 }
 function onFail(sender, args) {
 alert('Query failed. Error: ' + args.get_message());
 }  

There is a way to make this function reusable? Something like:

var FirstUserId  = getUserID('superman@superheroes.com');

var SecondUserId  = getUserID('batman@superheroes.com');

Thanks to everybody Marco

0
   **Your function should look like this**

     function getUserId(loginName) {
           //Declare your deferred object here  
            var deferred=$.Deferred(); 

            var context = new SP.ClientContext.get_current();
            user = context.get_web().ensureUser(loginName);
            context.load(user);
            context.executeQueryAsync(
            Function.createDelegate(null, ensureUserSuccess),
            Function.createDelegate(null, onFail));

           function ensureUserSuccess() {
             //Resolve your object here  
             deferred.resolve(this.user.get_id()); 
           }


           function onFail(sender, args) {
             //Reject your object here  
             console.log('An error occured while retrieving list items:' + args.get_message());
             deferred.reject(this.user.get_id());  
          }  

         //Return your Promise Object  
         return deferred.promise(); 
   }

Call function:

$('#userTemp').html(getUserID('superman@superheroes.com'))

REFERENCE

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.