简体   繁体   English

前提C#ClientContext Caml查询中的SharePoint 2013不过滤结果

[英]SharePoint 2013 On Premise C# ClientContext Caml Query not filtering results

I'm using CSOM C# ClientContext search on an on-premise app in order to query a certain list, however when my query goes through it receives all the items. 我在本地应用程序上使用CSOM C#ClientContext搜索以查询特定列表,但是当我的查询通过时,它将接收所有项目。 However I want a single query, a query that is supplied by a use of values store in a label which itself is taken from another sharepoint list. 但是,我需要一个查询,即使用值存储在标签中的查询,该标签本身取自另一个共享点列表。 So I'm wondering what part of my CamlQuery is wrong because everything else works. 所以我想知道我的CamlQuery的哪一部分是错误的,因为其他所有内容都可以。

using (ClientContext cdmContext = new ClientContext(url)) 使用(ClientContext cdmContext = new 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);


                            }
                        }

If I understand, you want to get all items who have the value of field 'ClassName' between 4 values (label1,label2,label3 or label4). 据我了解,您想获取所有字段'ClassName'的值介于4个值(label1,label2,label3或label4)之间的项目。 Test whil this CAML : You need to ADD View : 测试此CAML:您需要添加视图

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

You set ViewXML of your object;) 您设置对象的ViewXML ;)

If you request with your AND is wrong, test with an OR like : 如果您要求的AND错误,请使用OR进行测试,例如:

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

The type of you field 'ClassName' is a Text (single line of text) ? 您的字段“ ClassName”的类型是文本(单行文本)吗? Else you can use some external tools to help you to build your Caml like : CAML Designer Or U2U Caml Query Builder 另外,您可以使用一些外部工具来帮助您构建Caml,例如: CAML DesignerU2U Caml Query Builder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM