How get items from folder in Sharepoint list using JavaScript?

kol1991 picture kol1991 · May 16, 2014 · Viewed 7.3k times · Source

I have a list of Sharepoint, and with a few folders. I need to get to the contents of these folders using JavaScript, and is best to select a folder only the item that is the column AddData date is equal to the current date. I would like to ask you for help. The only thing I managed to get it are:

 context = SP.ClientContext.get_current();
 var web = context.get_web();
 list = context.get_web().get_lists().getByTitle("ExchangeRateList");
 var camlString =
    "<View><ViewFields>" +
        "<FieldRef Name='Title' />" +
        "<FieldRef Name='Modified' />" +
        "<FieldRef Name='Created' />"+
    "</ViewFields></View>";

    var camlQuery = new SP.CamlQuery();
    camlQuery.View
    camlQuery.set_viewXml(camlString);
    allAnnouncements = list.getItems(camlQuery);
    var enumerator = allAnnouncements.getEnumerator();
    while (enumerator.moveNext()) {
            var announcement = enumerator.get_current();
            var title = announcement.get_item("Title")
    ...

announcement is my folder but how get items from this folder? And check AddData column?

Answer

Vadim Gremyachev picture Vadim Gremyachev · May 18, 2014
  • Specify CAML Today Element to render the current date in the format that is relative to the server's local time zone
  • Set Scope="RecursiveAll" for View element to include items under folder(s)

Query

<View Scope="RecursiveAll"> 
      <Query>
          <Where>
             <Eq>
                  <FieldRef Name="AddData" />  
                  <Value Type="DateTime">   
                    <Today />   
                  </Value>   
             </Eq>
          </Where>   
     </Query>
</View>

Example

var listTitle = 'Announcements';

  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var list = web.get_lists().getByTitle(listTitle);
  var listItems = list.getItems(createQuery());


  context.load(listItems);
  context.executeQueryAsync(
     function() {

        var c = listItems.get_count();  
        for(var i = 0; i< c;i++ ) {
          var listItem = listItems.getItemAtIndex(i);
          console.log(listItem.get_item('Title')); 
          //...
        }  
     },
     function(sender,args){
        console.log(args.get_message());
     }
  );



  function createQuery()
  {
    var qry = new SP.CamlQuery();
    qry.set_viewXml('<View Scope="RecursiveAll"> \
                        <Query>  \
                           <Where>  \
                             <Eq>   \
                               <FieldRef Name="AddData" />  \
                               <Value Type="DateTime">   \
                                 <Today />   \
                               </Value>   \
                             </Eq>  \
                           </Where>   \
                        </Query>  \
                     </View>');
    return qry;
  }