简体   繁体   English

动态创建CAML查询

[英]Dynamically create CAML query

I have a serach page which earlier was function on SQL DB but now we moved the back-en to SharePoint, I am trying to build a query based upon user select from drop-down box. 我有一个先前在SQL DB上运行的serach页面,但现在我们将back-en移动到SharePoint,我正在尝试根据用户从下拉框中选择来构建查询。

Existing SQL query was : 现有的SQL查询是:

string SQLquery "Select companyname,phone,email from Tab where Approved = 1"

If (country.selectedindex != "")
{
   SQLquery += "AND (country LIKE '%" + country.SelectedValue + "%')"
}
If (functional.selectedindex != "")
{
   SQL += "AND (country LIKE '%" + country.SelectedValue + "%')"
}
If (state.selectedindex != "")
{
   SQL += "AND (state LIKE '%" + state.SelectedValue + "%') OR ( businessareaState like '%" + state.SelectedValue + "%'))"
}

This was easy, but I have to create the same query in CAML based upon user selection form the drop-down lists. 这很简单,但我必须根据下拉列表中的用户选择在CAML中创建相同的查询。 Somehow I am not able to give it a dynamic shape since the structure changes completely in the CAML as soon as you add and criteria in it. 不知怎的,我无法给它一个动态的形状,因为只要你在其中添加和标准,结构就会在CAML中完全改变。

Help would be highly appreciated. 帮助将受到高度赞赏。

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. 您可以创建自己的对象并传递给此方法。

You can use this free tool to help build your CAML queries 您可以使用此免费工具来帮助构建CAML查询

http://www.u2u.be/res/tools/camlquerybuilder.aspx http://www.u2u.be/res/tools/camlquerybuilder.aspx

Do a few variations on your query and you will see how the structure changes. 对您的查询做一些变化,您将看到结构如何变化。 Basically you will have to build up an XML document rather than use string concatenation (though that can work as well, it will probably be simpler to build it in an XML parser) 基本上你必须建立一个XML文档而不是使用字符串连接(虽然它也可以工作,但在XML解析器中构建它可能更简单)

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

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