是不是感觉写一个CAML的查询有点麻烦? 尤其是在写复杂查询的时候. 在这里跟大家分享一个codeplex上的工具, 专门用来帮你拼CAML查询字符串的.
下载地址:
SharePoint CAML Helper
http://sharepointcamlhelper.codeplex.com/
使用步骤:
1. 下载CAMLHelper.dll
2. 新建一个C#的Console Application.
3. 添加对该dll的引用.
4. 粘贴下面的示例代码.
using System; using System.Collections.Generic; using System.Text; using StooRob.CAMLHelper.CAML; namespace CAMLHelperTest { class Program { static void Main(string[] args) { CAMLManager target = new CAMLManager( new QueryGroup( "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "List Item Title" ) ); target.QueryGroups.Add( new QueryGroup( Types.FieldRefTypes.ID, "{D562C8AC-457A-446b-84D6-2FA26FAABED7}", Types.FieldTypes.YesNo, Types.QueryTypes.Neq, "true" ) ); target.OrderBy.Add(new OrderBy("Title", false)); target.ViewFields.Add(new ViewField("Title")); target.ViewFields.Add( new ViewField( "{D562C8AC-457A-446b-84D6-2FA26FAABED7}", Types.FieldRefTypes.ID ) ); target.RowLimit = 100; string camlQuery = target.GetFullCAML(); Console.WriteLine(camlQuery); Console.ReadLine(); } } }
程序输出结果(未整理):
<Where><And><Eq><FieldRef Name="Title" /><Value Type="Text">List Item Title</Val
ue></Eq><Neq><FieldRef ID="{D562C8AC-457A-446b-84D6-2FA26FAABED7}" /><Value Type
="YesNo">true</Value></Neq></And></Where><OrderBy><FieldRef Name="Title" Ascendi
ng="False" /></OrderBy><ViewFields><FieldRef Name="Title" /><FieldRef ID="{D562C
8AC-457A-446b-84D6-2FA26FAABED7}" /></ViewFields><RowLimit>100</RowLimit>
整理后看起来如下:
<Where> <And> <Eq> <FieldRef Name="Title" /> <Value Type="Text">List Item Title</Value> </Eq> <Neq> <FieldRef ID="{D562C8AC-457A-446b-84D6-2FA26FAABED7}" /> <Value Type="YesNo">true</Value> </Neq> </And> </Where> <OrderBy> <FieldRef Name="Title" Ascending="False" /> </OrderBy> <ViewFields> <FieldRef Name="Title" /> <FieldRef ID="{D562C8AC-457A-446b-84D6-2FA26FAABED7}" /> </ViewFields> <RowLimit>100</RowLimit>
注: codeplex上有该工具的源代码. 可以供学习参考之用.
链接:
SharePoint CAML Helper