2

I want to select all items from a list where Title is equal to logInUser (which is current login user name).

In my code I have created : var logInUser = context1.get_web().get_currentUser();

But this doesn't work. Anyone can help? For my caml query ?? Thanks in advanced.

SP.SOD.executeFunc('sp.js','SP.ClientContext',readyFunction);

function readyFunction()  {
   getCurrentLoginUser();
   getCurrentLoginUserDetails();
}

function getCurrentLoginUser() {
   context = new SP.ClientContext.get_current(); 
   web = context.get_web(); 
   currentUser = web.get_currentUser();
   currentUser.retrieve();
   context.load(web); 
   context.executeQueryAsync(onQuerySucceeded,onQueryFailed);
}

function getCurrentLoginUserDetails() {
   context1 = new SP.ClientContext.get_current();
   oList = context1.get_web().get_lists().getByTitle('Employee Annual Leave Records');
   var logInUser = context1.get_web().get_currentUser();
   var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml(   
     '<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + 
     '<Value Type=\'Text\'>' + logInUser + '</Value></Eq></Where>' +
     '</Query></View>'     
   );
   collListItem = oList.getItems(camlQuery);
   context1.load(collListItem);
   context1.executeQueryAsync(onQuerySucceededD,onQueryFailedD);
}

function onQuerySucceeded(sender , args) { 
   var user = web.get_currentUser();
   fd.field('Employee_x0020_Name').control().value(user.get_title());
}

function onQueryFailed(sender , args) {
   alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());   
}

function onQuerySucceededD(sender , args) {

   var listItemEnumerator = collListItem.getEnumerator();

   while (listItemEnumerator.moveNext()) {
      var oListItem = listItemEnumerator.get_current();
      fd.field('Employee_x0020_Number').value(oListItem.get_item('Employee_x0020_Number'));
      fd.field('Employed_x0020_Date').value(oListItem.get_item('Employed_x0020_Date'));
    }
}

function onQueryFailedD(sender , args) {
   alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
1
  • Can you add the error message you receive? Also try to change you escaped ' to " instead. Also did you check that you are providing what you expect to provide with the logInUser variable? as that is an SP.User object while your testing it against a Text field.
    – Robban1980
    Commented Apr 22, 2015 at 2:20

1 Answer 1

1

We need to be careful about the Async calls. I would suggest don't call getCurrentLoginUserDetails() immediately, Let getCurrentUser() complete its async call and then you can proceed with your function to retrieve list items based on LoggedInUser.

So based on above explanation, I can propose following solution.

Improvements in your function getCurrentLoginUserDetails: declare collListItem as this.collListItem or declare it as a global variable.

1. This is the way how you initiate your function calls

function readyFunction()  {
   getCurrentUser();
}

2. First get current user name and store it in a variable.

var LoggedInUser = '';
function getCurrentUser () {
try {
       var clientContext = new SP.ClientContext.get_current();
       var currentUser = clientContext.get_web().get_currentUser();
       clientContext.load(currentUser); 
       clientContext.executeQueryAsync(function () {
       var index = currentUser.get_loginName().indexOf('|') + 1;

       //Value of logged in user will be like: DomainName\UserName. So on how value 
       //is stored in Title field of your list you need to get User Name 
       //for e.g. if you don't need DomainName just remove it.

       LoggedInUser = currentUser.get_loginName().substring(index);

       //You can also get some extra information if needed like ID as below
       //currentUser.get_id();

       //Once you have logged in user, then call getCurrentLoginUserDetails
       getCurrentLoginUserDetails();

     }, queryFailure);
    }
    catch (err) {
       alert('Error');
    }
}

function queryFailure (sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

3. Now once logged in user is fetched then call below function

var collListItem = '';
function getCurrentLoginUserDetails() {
   context1 = new SP.ClientContext.get_current();
   oList = context1.get_web().get_lists().getByTitle('Employee Annual Leave Records');

   // User already fetched logged in user name from previous function
   var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml(   
 '<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + 
 '<Value Type=\'Text\'>' + LoggedInUser + '</Value></Eq></Where>' +
 '</Query></View>'     
   );
   collListItem = oList.getItems(camlQuery);
   context1.load(collListItem);
   context1.executeQueryAsync(onQuerySucceededD, queryFailure);
}

function onQuerySucceededD() {
   var listItemEnumerator = collListItem.getEnumerator();
   while (listItemEnumerator.moveNext()) {
      var oListItem = listItemEnumerator.get_current();
    fd.field('Employee_x0020_Number').value(oListItem.get_item('Employee_x0020_Number'));
    fd.field('Employed_x0020_Date').value(oListItem.get_item('Employed_x0020_Date'));
    }
}

So calling functions in sequence will help solving this issue

1
  • Most welcome. :)
    – Asad Refai
    Commented Apr 22, 2015 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.