What is wrong with this caml query to get exception “can not complete this action. Please try again”?

杀马特。学长 韩版系。学妹 提交于 2020-03-05 01:30:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!