我在内部应用上使用CSOM C#ClientContext搜索以查询某个列表,但是当我的查询通过时,它会收到所有项目。但是我想要一个单一的查询,一个由值使用值提供的查询存储在一个标签中,该标签本身是从另一个共享点列表中获取的。所以我想知道我的CamlQuery的哪一部分是错的,因为其他一切都有效。
使用(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);
}
}
答案 0 :(得分:0)
如果我理解,您希望获得具有字段&#39; ClassName&#39; 4个值之间(label1,label2,label3或label4)。 测试这个CAML: 您需要添加查看:
<View> [The CAML Query]</View>
您设置对象的 ViewXML ;)
如果您使用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>
您的字段类型&#39; ClassName&#39;是一个文本(单行文本)? 另外,您可以使用一些外部工具来帮助您构建Caml:CAML Designer 或U2U Caml Query Builder