AB.*_*AB. 1 c# sharepoint caml
我想基于查询字符串动态caml查询.让我用例子解释一下
我的查询刺痛可以是任何东西
?cat=ABC&cat=ABD&cat=ABE...
?Cat=ABC
?Cat=ABC&cat=ABL
Run Code Online (Sandbox Code Playgroud)
所以不行.现在可以解决问题了
我想根据此查询字符串查询我的sharepoint列表
if (HttpContext.Current.Request.QueryString["cat"] != null)
{
string _cat = HttpContext.Current.Request.QueryString["cat"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
所以这样我的字符串包含所有查询
string _cat=ABC,AD,....all.
Run Code Online (Sandbox Code Playgroud)
我想根据这些查询字符串和"AND"查询我的sharepoint列表
where title=ABC and title=AD ....
Run Code Online (Sandbox Code Playgroud)
如果只有一个查询字符串,那么只有title=ABC....所以我希望我的查询字符串应该是动态的...任何想法如何实现这一点?
假设您正在谈论多选选择字段...您很可能每次都必须创建查询.
您的代码需要确定传入的类别数,然后生成CAML.例如,如果仅传递ABC,则查询将是(注意没有<And>标记):
<Where>
<Eq>
<FieldRef Name='Category'/>
<Value Type='Choice'>ABC</Value>
</Eq>
</Where>
Run Code Online (Sandbox Code Playgroud)
但是如果你有三个选择通过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>
Run Code Online (Sandbox Code Playgroud)
编辑:
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>");
}
Run Code Online (Sandbox Code Playgroud)