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

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);
     }));



   });

}
  • 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" – Varsha Aug 3 '17 at 6:59
  • Just a typo probably, you should push it to the returnedItemsIdeas array – Anders Aune Aug 3 '17 at 7:25
  • @AndersAune: yes. I fixed that. – Nils Aug 3 '17 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 – Varsha Aug 3 '17 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. – Nils Aug 3 '17 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, privacy policy and cookie policy

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