2016-08-23 34 views
0

我正在嘗試爲Sharepoint 2013編寫一個簡單的Web部件,用於搜索特定內容類型的所有頁面並輸出其標題列表。我正在使用CAML查詢來搜索。但是無論查詢什麼,我的結果都只是這個Sharepoint站點中所有文件和文件夾的列表。CAML搜索頁面將返回站點中的所有文件和文件夾

最後,我將CAML查詢簡化爲一個簡單的「查找以字母T開頭的所有內容」,但結果仍然是輸出站點根級中的所有文件和文件夾。

我在做什麼錯?

protected override void CreateChildControls() 
    { 
     Label label1 = new Label(); 

     try 
     { 
      SPQuery query = new SPQuery(); 
      query.Query = @"<Query> 
          <Where> 
           <BeginsWith> 
           <FieldRef Name='Title'></FieldRef> 
           <Value Type='Text'>T</Value> 
           </BeginsWith> 
          </Where> 
          </Query>"; 

      using (SPSite site = new SPSite("https://xxxxxx/sites/xxxxx/en/xxxx/")) 
      { 
       using (SPWeb web = site.OpenWeb()) 
       { 
        PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web); 
        PublishingPageCollection collection = pubweb.GetPublishingPages(query); 

        //now output the results of the query       
        label1.Text = "Items: " + collection.Count.ToString(); 
        for (int i = 0; i < collection.Count; i++) 
        { 
         label1.Text += collection[i].Title + "<br>"; 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      label1.Text = ex.Message; 
     } 


     Controls.Add(label1); 
    } 

回答

0

古怪,你需要做兩件事情:

1)拆下打開和關閉標籤;

2)確保查詢不以空行開始或結束。

然後結果會改變正確的。

相關問題