Sharepoint动态caml查询问题?

发布于 2024-08-16 07:34:26 字数 662 浏览 1 评论 0原文

我想基于查询字符串进行动态 caml 查询。让我用示例来解释一下,

我的查询字符串可以是任何内容

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

,所以不是。现在问题开始了

我想根据这个查询字符串查询我的共享点列表,

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

这样我的字符串包含

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

我想根据这些查询字符串查询我的共享点列表的所有查询,

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

如果只有一个,则 使用“AND”查询字符串然后仅 其中 title=ABC....所以我希望我的查询字符串应该是动态的.... 知道如何实现这一点吗?

I want to dynamic caml query based on query string.Let me explain it with example

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"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那请放手 2024-08-23 07:34:26

假设您正在谈论多选选择字段...很可能您每次都必须创建查询。

您的代码需要确定传入的类别数量,然后生成 CAML。例如,如果仅传递 ABC,您的查询将是(注意没有 标签):

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

但是如果您通过 QueryString 传递了三个选择:ABC、ABD 和 ABE,您将get(注意每两个组周围有 标签):

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

编辑:

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>");
}

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. For example, if only ABC is passed your query would be (notice there are no <And> tags):

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

<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>");
}
生寂 2024-08-23 07:34:26

检查 http://camlex.codeplex.com 项目。它的创建正是为了简化使用 C# lambda 表达式创建动态 CAML 语句。另请查看我的帖子:基于以下内容构建动态 CAML 查询查询字符串参数

check http://camlex.codeplex.com project. It was created exactly for simplifying of creation of dynamic CAML statements using C# lambda expressions. Also check my post: Build dynamic CAML queries based on query string parameters

会发光的星星闪亮亮i 2024-08-23 07:34:26

我开发了 C# 代码来构建动态查询。
就像这样

 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 是保存过滤器元素的自定义对象。您可以创建自己的对象并传递给此方法。

I have developed C# code to build dynamic query.
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. You can create your own object and pass into this method.

以往的大感动 2024-08-23 07:34:26

当您有多个输入时创建 CAML 查询字符串的基本算法是:

  • 如果只有一个值要检查,则不需要 ,只需创建代码
  • 如果您有两个值,您将需要 (value1)(value2)
  • 如果您有两个以上,则创建一个循环(伪代码,抱歉):

    foreach(值中的项目)
     sQuery =“<和>” + sQuery + item + ""
    结束foreach
    

The basic algorithm for creating the CAML query string when you have multiple inputs is:

  • If there is only one value to check, you don't need the <And>, just create the code
  • If you have two values, you will need <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
    
烛影斜 2024-08-23 07:34:26

如果您讨厌使用 String Concat 方法,您必须尝试 JohnHolliday 的 Lib - CAML.NET,我在我的项目中使用它,它非常棒。

你也会喜欢的

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.

You too will love it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文