2016-09-15 83 views
0

我在內部部署應用程序上使用CSOM C#ClientContext進行搜索以查詢某個列表,但是當我的查詢經過時它會接收所有項目。不過,我想要一個查詢,這是一個使用值存儲提供的查詢,該查詢本身是從另一個共享點列表中獲取的。所以我想知道我的CamlQuery的哪一部分是錯誤的,因爲一切正常。SharePoint 2013 On Premise C#ClientContext Caml查詢不會篩選結果

使用(ClientContext cdmContext =新的ClientContext(URL))

    { 
         cdmContext.Credentials = fabrikam_credentials; 

         Web cdmWeb = cdmContext.Web; 

         List cdmList = cdmWeb.Lists.GetByTitle("ListOfClasses"); 

         if (cdmList == null) return; 

         CamlQuery cdmQuery = new CamlQuery(); 
         cdmQuery.ViewXml = "<Query><Where><And><Or><Eq><FieldRef Name='ClassName'></FieldRef><Value Type='Text'>" + Label1.Text + "</Value></Eq><Eq><FieldRef Name='ClassName'></FieldRef><Value Type='Text'>" + Label2.Text + "</Value></Eq></Or><Or><Eq><FieldRef Name='ClassName'></FieldRef><Value Type='Text'>" + Label3.Text + "</Value></Eq><Eq><FieldRef Name='ClassName'></FieldRef><Value Type='Text'>" + Label4.Text + "</Value></Eq></Or></And></Where></Query>"; 
         ListItemCollection cdmItems = cdmList.GetItems(cdmQuery); 
         if (cdmItems == null) return; 

         cdmContext.Load(cdmItems); 
         cdmContext.ExecuteQuery(); 

         if (cdmItems != null) 
         { 
          foreach (ListItem cdmItem in cdmItems) 
          { 
           string cClassName = cdmItem["ClassName"].ToString(); 
           string cInstructorName = "Professor Buckman"; 
           DateTime cStartDate = DateTime.Parse(cdmItem["StartDate"].ToString()); 
           DateTime cEndDate = DateTime.Parse(cdmItem["_EndDate"].ToString()); 
           int cRoomNumber = Int32.Parse(cdmItem["ClassNumber"].ToString()); 
           string cDayOfTheWeek = cStartDate.DayOfWeek.ToString(); 

           System.Web.UI.WebControls.TableRow fuRow1 = new System.Web.UI.WebControls.TableRow(); 
           System.Web.UI.WebControls.TableCell fuR1C1 = new System.Web.UI.WebControls.TableCell(); 
           fuR1C1.Text = cClassName; 
           System.Web.UI.WebControls.TableCell fuR1C2 = new System.Web.UI.WebControls.TableCell(); 
           fuR1C2.Text = cRoomNumber.ToString(); 
           System.Web.UI.WebControls.TableCell fuR1C3 = new System.Web.UI.WebControls.TableCell(); 
           fuR1C3.Text = cStartDate.ToString(); 
           fuRow1.Cells.Add(fuR1C1); 
           fuRow1.Cells.Add(fuR1C2); 
           fuRow1.Cells.Add(fuR1C3); 
           FUScheduler.Rows.Add(fuRow1); 
           System.Web.UI.WebControls.TableRow fuRow2 = new System.Web.UI.WebControls.TableRow(); 
           System.Web.UI.WebControls.TableCell fuR2C1 = new System.Web.UI.WebControls.TableCell(); 
           fuR2C1.Text = cInstructorName; 
           System.Web.UI.WebControls.TableCell fuR2C2 = new System.Web.UI.WebControls.TableCell(); 
           fuR2C2.Text = cDayOfTheWeek; 
           System.Web.UI.WebControls.TableCell fuR2C3 = new System.Web.UI.WebControls.TableCell(); 
           fuR2C3.Text = cStartDate.ToString(); 
           fuRow2.Cells.Add(fuR2C1); 
           fuRow2.Cells.Add(fuR2C2); 
           fuRow2.Cells.Add(fuR2C3); 
           FUScheduler.Rows.Add(fuRow2); 


          } 
         } 

回答

0

如果我明白了,你想要得到誰擁有場的4個值(LABEL1,LABEL2,LABEL3之間的值 '類名' 所有項目或標籤4)。 測試得到控制而這個CAML: 你需要添加查看

<View> [The CAML Query]</View> 

您設置ViewXML你的對象的

如果你與你的請求,是錯誤的,測試用或類似:

<View> 
<Query> 
    <Where> 
     <Or> 
     <Eq> 
      <FieldRef Name='ClassName' /> 
      <Value Type='Text'>" + Label1.Text + "</Value> 
     </Eq> 
     <Or> 
      <Eq> 
       <FieldRef Name='ClassName' /> 
       <Value Type='Text'>" + Label2.Text + "</Value> 
      </Eq> 
      <Or> 
       <Eq> 
        <FieldRef Name='ClassName' /> 
        <Value Type='Text'>" + Label3.Text + "</Value> 
       </Eq> 
       <Eq> 
        <FieldRef Name='ClassName' /> 
        <Value Type='Text'>" + Label4.Text + "</Value> 
       </Eq> 
      </Or> 
     </Or> 
     </Or> 
    </Where> 
</Query> 
</View> 

你的字段'ClassName'的類型是一個文本(單行文本)? 否則您可以使用一些外部工具來幫助您構建您的Caml:CAML DesignerU2U Caml Query Builder