问题
I am implementing a caml query on a SharePoint list! I have 5 conditions and I place every 2 condition in one tag. but It is still get this exception: "can not complete this action. please try again!"
<And>
<Eq>
<FieldRef Name='fieldName1' />
<Value Type='Text' >value1</Value>
</Eq>
<And>
<Contains>
<FieldRef Name='fieldName2' />
<Value Type='Text' >value2</Value>
</Contains>
<Contains>
<FieldRef Name='fieldName3' />
<Value Type='Text' >value3</Value>
</Contains>
</And>
<And>
<Eq>
<FieldRef Name='fieldName4' />
<Value Type='DateTime' IncludeTimeValue='false'>2019-06-22</Value>
</Eq>
<Eq>
<FieldRef Name='fieldName5' />
<Value Type='DateTime' IncludeTimeValue='false'>2019-05-06</Value>
</Eq>
</And>
</And>
what is wrong with my query?
回答1:
For comparisons involving multiple fields, it's more like bottom up approach, you start comparing 2 fields, then the result with 3rd field then the result with 4th field and so on. So, your query should be like:
<And>
<Eq>
<FieldRef Name='fieldName1' />
<Value Type='Text'>value1</Value>
</Eq>
<And>
<Contains>
<FieldRef Name='fieldName2' />
<Value Type='Text'>value2</Value>
</Contains>
<And>
<Contains>
<FieldRef Name='fieldName3' />
<Value Type='Note'>value3</Value>
</Contains>
<And>
<Eq>
<FieldRef Name='fieldName4' />
<Value Type='Text'>value4</Value>
</Eq>
<Eq>
<FieldRef Name='fieldName5' />
<Value Type='Text'>value5</Value>
</Eq>
</And>
</And>
</And>
</And>
回答2:
I got my fault and make my query with a recursive algorithm. I am pleased if someone helps to improve it productivity:
public static void main()
{
string NestedQuery = "";
int NestedQueryCounter = 0;
string query = "<View><Query><Where>";
var whereClauses = new List<string>();
whereClauses.Add(SetupQueryExpression("FirstFiled", FirstValue, "Text", "Eq"));
whereClauses.Add(SetupQueryExpression("SecondFiled",SecondValue, "Text", "Contains"));
whereClauses.Add(SetupQueryExpression("ThirdField", ThirdValue, "Text", "Contains"));
whereClauses.Add(SetupQueryExpression("ForthField", ForthValue, "DateTime", "Eq", "IncludeTimeValue='false'"));
whereClauses.Add(SetupQueryExpression("FifthField",FifthValue, "DateTime", "Eq", "IncludeTimeValue='false'"));
NestedWhereClauses(whereClauses, "<And>", "</And>");
query += NestedQuery;
query += "</Where></Query></View>";
}
private string SetupQueryExpression(string fieldName, string fieldValue, string dataType, string condition,
string extraValueCondition = "")
{
string query = @"<{0}>
<FieldRef Name='{1}' /><Value Type='{2}' {4}>{3}</Value>
</{0}>";
return string.Format(query, condition, fieldName, dataType, fieldValue, extraValueCondition);
}
private void NestedWhereClauses(List<string> whereCalauses, string mainCondition, string endMainCondition)
{
if (2 == whereCalauses.Count)
{
NestedQuery += mainCondition;
NestedQuery += whereCalauses[0];
NestedQuery += whereCalauses[1];
NestedQuery += endMainCondition;
for (int counter = 0; counter < NestedQueryCounter; counter++)
NestedQuery += endMainCondition;
}
else
{
NestedQueryCounter++;
NestedQuery += mainCondition;
NestedQuery += whereCalauses[0];
whereCalauses.RemoveAt(0);
NestedWhereClauses(whereCalauses, mainCondition, endMainCondition);
}
}
来源:https://stackoverflow.com/questions/60374836/what-is-wrong-with-this-caml-query-to-get-exception-can-not-complete-this-actio