2

Below is my code .

I want to pass ID's in CAML query and get respective title.

totalID=[1,2,3];
function GetTitleIdea()
{  
   var promise = GetItemId();
   promise.done(function()
   {  
     for(var i=0;i<totalID.length;i++)  
     {   
         var contextIdeas = new SP.ClientContext();          
         var list = contextIdeas.get_web().get_lists().getByTitle('Ideas');          
         var caml = new SP.CamlQuery();           
         caml.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID'/><Value 
         Type='Integer'>"+totalID[i]+"</Value></Eq></Where></Query></View>");         
         var returnedItemsIdeas = list.getItems(caml);  
         contextIdeas.load(returnedItemsIdeas);      
         contextIdeas.executeQueryAsync(
         Function.createDelegate(this,
         function()
         {
             var enumeratorIdeas = returnedItemsIdeas.getEnumerator();
             while (enumeratorIdeas.moveNext()) 
             {              
                  var listItemIdeas =enumeratorIdeas.get_current(); 
                  Title=listItemIdeas.get_item('Title');
                  console.log(Title);
             }

         }),
         Function.createDelegate(this,
         function (sender, args) 
         { 
              Console.log(err.message);
         }));

      }

   });

}
3
  • 2
    do you want to use JSOM only? There is a simpler solution via REST API Commented Aug 3, 2017 at 5:03
  • Any reason you don't use the IN operator in the CAML query? msdn.microsoft.com/en-us/library/office/ff625761.aspx Commented Aug 3, 2017 at 6:19
  • Yes I want to use JSOM only Commented Aug 3, 2017 at 7:07

2 Answers 2

3

The problem here is that you modify returnedItemsIdeas within a loop and then call executeQueryAsync - probably expecting returnedItemsIdeas to still have the same value when executeQueryAsync callbacks fire.
This is not how things work in JavaScript.

You have to "remember" all items that you load and iterate them when then loading is done. I have modified your code to reflect what i mean - but it is not tested:

function GetTitleIdea()
{  
   var promise = GetItemId(),
       returnedItemsIdeas = [],
       contextIdeas = SP.ClientContext.get_current(),
       list = contextIdeas.get_web().get_lists().getByTitle('Ideas'),
       caml = new SP.CamlQuery(),
       i, ideaToLoad;
   promise.done(function()
   {  
     for(i=0;i<totalID.length;i++)  
     {   
         caml.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID'/><Value 
         Type='Integer'>"+totalID[i]+"</Value></Eq></Where></Query></View>");         
         ideaToLoad = list.getItems(caml);  
         contextIdeas.load(ideaToLoad); 
         returnedItemsIdeas.push(ideaToLoad);
     }       

     contextIdeas.executeQueryAsync(
     Function.createDelegate(this,
     function()
     {
         var enumeratorIdeas,listItemIdeas, title;
         for(i=0; i<returnedItemsIdeas.length; i++)
         {
             enumeratorIdeas = returnedItemsIdeas[i].getEnumerator();
             while (enumeratorIdeas.moveNext()) 
             {              
                  listItemIdeas =enumeratorIdeas.get_current(); 
                  title=listItemIdeas.get_item('Title');
                  console.log(title);
             }
         }

     }),
     Function.createDelegate(this,
     function (sender, args) 
     { 
          Console.log(err.message);
     }));



   });

}
6
  • Thank You for quick reply.I tried your method but i got error for contextIdeas.push. I got below error "contextIdeas.push is not a function" Commented Aug 3, 2017 at 6:59
  • Just a typo probably, you should push it to the returnedItemsIdeas array Commented Aug 3, 2017 at 7:25
  • @AndersAune: yes. I fixed that. Commented Aug 3, 2017 at 7:28
  • When I used above code I get following error. "a.get_$19_0 is not a function" Nothing will happen after that.Thank you Commented Aug 3, 2017 at 9:44
  • I have modifed contextIdeas.load(ideaToLoad); to contextIdeas.load(ideaToLoad, "Title"); so that the title-field will be included in the loading and not only the item. Also I have modified listItemIdeas.get_item('Title'); to listItemIdeas.item('Title'); - I guess the latter was causing the problem. Commented Aug 3, 2017 at 9:59
1

Here we should use Recursion process.

Place the code in a function and pass the array[index] ( your required value) as parameter to that function. In success method cal the same function with Incremental index, continue this until reach the index count to array length.

If you want continue even in fail case cal the method in Failure function.

Hope this helps you :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.