如何使用JavaScript从Sharepoint列表中的文件夹获取项目?

时间:2014-05-16 15:43:53

标签: javascript sharepoint sharepoint-2013 splistitem

我有一个Sharepoint列表,还有一些文件夹。我需要使用JavaScript来获取这些文件夹的内容,并且最好只选择一个文件夹,即AddData日期列等于当前日期。我想请你帮忙。我唯一能做到的就是:

 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")
    ...

公告是我的文件夹,但如何从此文件夹中获取项目?并检查AddData列?

1 个答案:

答案 0 :(得分:0)

  • 指定CAML Today Element以格式呈现当前日期 这是相对于服务器的本地时区
  • Scope="RecursiveAll"设置View element以包含项目 文件夹(S)

查询

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

实施例

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