简体   繁体   English

Sharepoint动态caml查询问题?

[英]Sharepoint dynamic caml query problem?

I want to dynamic caml query based on query string.Let me explain it with example 我想基于查询字符串进行动态caml查询。

my query sting can be anything 我的查询字符串可以是任何东西

?cat=ABC&cat=ABD&cat=ABE...
?Cat=ABC
?Cat=ABC&cat=ABL

so no. 所以不行。 can be anything now the problem begins 可能是什么,现在问题开始了

I want to query my sharepoint list based on this query string 我想根据此查询字符串查询我的共享点列表

if (HttpContext.Current.Request.QueryString["cat"] != null)
        {
            string _cat = HttpContext.Current.Request.QueryString["cat"].ToString();
        }

so this way my string contains all the query 所以我的字符串包含所有查询

string _cat=ABC,AD,....all.

I want to query my sharepoint list based on these query string and with "AND" 我想基于这些查询字符串并使用“ AND”查询我的共享点列表

where title=ABC and title=AD ....

if there is only one query string then only where title=ABC ....so I want my query string should be dynamic.... Any idea how to acheive this?? 如果只有一个查询字符串,那么只有title=ABC ....所以我希望我的查询字符串应该是动态的。...知道如何实现这一点吗?

Assuming you are talking about a Multi-select choice field... most likely you will have to create the query each time. 假设您正在谈论“多选选择”字段...最有可能您每次都必须创建查询。

Your code is going to need to determine how many categories are passed in and then generate the CAML. 您的代码将需要确定传入的类别数,然后生成CAML。 For example, if only ABC is passed your query would be (notice there are no <And> tags): 例如,如果仅传递ABC,您的查询将是(注意没有<And>标签):

<Where>
  <Eq>
    <FieldRef Name='Category'/>
    <Value Type='Choice'>ABC</Value>
  </Eq>
</Where>

But if you have three choices passed in via QueryString: ABC, ABD, and ABE you would get (notice the <And> tags surround each group of two): 但是,如果您通过QueryString传递了三个选择:ABC,ABD和ABE,您将会得到(请注意, <And>标记围绕两组的每一个):

<Where>
  <And>
    <And>
      <Eq>
        <FieldRef Name='Category'/>
        <Value Type='Choice'>ABC</Value>
      </Eq>
      <Eq>
        <FieldRef Name='Category'/>
        <Value Type='Choice'>ABD</Value>
      </Eq>
    </And>
    <Eq>
      <FieldRef Name='Category'/>
      <Value Type='Choice'>ABE</Value>
    </Eq>
  </And>
</Where>

Edit: 编辑:

static void Main(string[] args)
{
    try
    {
        string[] parameters = { "ABC", "DEF", "GHI" };
        string camlQuery = CreateCAMLQuery(parameters);
        Console.WriteLine(camlQuery);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    Console.WriteLine("Press any key...");
    Console.ReadKey();
}

private static string CreateCAMLQuery(string[] parameters)
{
    StringBuilder sb = new StringBuilder();

    if (parameters.Length == 0)
    {
        // perhaps set a default query?
        AppendEQ(sb, "all");
    }

    // "AND" each parameter to the query
    for (int i = 0; i < parameters.Length; i++)
    {
        AppendEQ(sb, parameters[i]);

        if (i > 0)
        {
            sb.Insert(0, "<And>");
            sb.Append("</And>");
        }
    }

    sb.Insert(0, "<Where>");
    sb.Append("</Where>");

    return sb.ToString();
}

private static void AppendEQ(StringBuilder sb, string value)
{
    // put your field's internal name in place of Category
    sb.Append("<Eq>");
    sb.Append("<FieldRef Name='Category'/>");
    sb.AppendFormat("<Value Type='Choice'>{0}</Value>", value);
    sb.Append("</Eq>");
}

check http://camlex.codeplex.com project. 检查http://camlex.codeplex.com项目。 It was created exactly for simplifying of creation of dynamic CAML statements using C# lambda expressions. 正是为了简化使用C#lambda表达式创建动态CAML语句而创建的。 Also check my post: Build dynamic CAML queries based on query string parameters 还要检查我的文章: 基于查询字符串参数构建动态CAML查询

I have developed C# code to build dynamic query. 我已经开发了C#代码来构建动态查询。 Its like this 就像这样

 public string GenerateQuery(IList<CamlQueryElements> lstOfElement)
    {
        StringBuilder queryJoin = new StringBuilder();
        string query = @"<{0}><FieldRef Name='{1}' /><Value {2} Type='{3}'>{4}</Value></Eq>";
        if (lstOfElement.Count > 0)
        {
            int itemCount = 0;
            foreach (CamlQueryElements element in lstOfElement)
            {
                itemCount++;
                string date = string.Empty;
                // Display only Date
                if (String.Compare(element.FieldType, "DateTime", true) == 0)
                    date = "IncludeTimeValue='false'";
                queryJoin.AppendFormat(string.Format(query, element.ComparisonOperators,
                                element.FieldName, date, element.FieldType, element.FieldValue));

                if (itemCount >= 2)
                {
                    queryJoin.Insert(0, string.Format("<{0}>", element.LogicalJoin));
                    queryJoin.Append(string.Format("</{0}>", element.LogicalJoin));
                }
            }
            queryJoin.Insert(0, "<Where>");
            queryJoin.Append("</Where>");
        }
        return queryJoin.ToString();
    }

IList lstOfElement is custom object which holds filter elements. IList lstOfElement是自定义对象,其中包含过滤器元素。 You can create your own object and pass into this method. 您可以创建自己的对象并将其传递给此方法。

The basic algorithm for creating the CAML query string when you have multiple inputs is: 有多个输入时创建CAML查询字符串的基本算法是:

  • If there is only one value to check, you don't need the <And> , just create the code 如果只检查一个值,则不需要<And> ,只需创建代码
  • If you have two values, you will need <and>(value1)(value2)</and> 如果您有两个值,则需要<and>(value1)(value2)</and>
  • If you have more than two, you create a loop (pseudocode, sorry): 如果您有两个以上,则创建一个循环(伪代码,对不起):

     foreach (item in values) sQuery = "<And>" + sQuery + item + "</And>" end foreach 

If you hate doing it using the String Concat method, You got to Try the JohnHolliday's Lib - CAML.NET , I use it in my project and it just rocks. 如果您不喜欢使用String Concat方法来执行此操作,则必须尝试JohnHolliday的Lib-CAML.NET ,我在项目中使用了它,但效果却很糟糕

You too will love it 你也会爱上它

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

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