SharePoint中的列表(List) 与Content Type 之三
SharePoint中的列表(List) 与Content Type 之三
对列表数据使用查询
你可以使用SPQuery对象来获得列表中的具体结果. 当使用SPQuery对象的时候, 你需要创建CAML语句来选择在目标列表中的具体数据. 为了选择已经过期的announcements , 你也许会需要像下面的例子一样使用CAML语句:
SPQuery query = new SPQuery();
query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Expires'/>";
query.Query =
@"<Where>
<Lt>
<FieldRef Name='Expires' />
<Value Type='DateTime'>
<Today /></Value>
</Lt>
</Where>";
SPList list = site.Lists["Litware News"];
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem expiredItem in items)
{
Console.WriteLine(expiredItem["Title"]);
}
你必须指明你想要在query中返回的fields, 方法是使用ViewFields 属性. 还要注意, 你必须使用field name来指定field, 而不是DisplayName. 如果你试图访问fields而不在ViewFields中指定, 你会遇到类型的ArgumentException的异常.
基本的query的语法是:
“<Where><operator><operand /><operand /></operator> </Where>”
下表列出了你会在query中使用的基本的CAML, 更完整的列表, 你需要查看SDK.
|
Element |
Description |
|
And |
聚合多个条件 |
|
BeginsWith |
在文本域中从头搜索字符串 |
|
Contains |
在文本域中搜索字符串 |
|
Eq |
相等 |
|
FieldRef |
引用一个field(对GroupBy元素很有用) |
|
Geq |
大于等于 |
|
GroupBy |
通过这些field来分组 |
|
Gt |
大于 |
|
IsNotNull |
非空 |
|
IsNull |
是空 |
|
Leq |
小于或等于 |
|
Lt |
小于 Less Than |
|
Neq |
不等于 |
|
Now |
当前的日期和时间 |
|
Or |
布尔或操作符 |
|
OrderBy |
排序query的结果 |
|
Today |
今天的日期 |
|
TodayIso |
ISO格式的今天的日期 |
|
Where |
用来指定查询的Where子句 |
SPQuery是一个从单个列表中拿数据的很棒的方法. 更进一步的是, 当你想要查找符合某种条件的列表项是, 使用SPQuery可以比遍历所有列表项的速度明显地加快. 然而, WSS 3.0引入了一种新的query机制, 引入的方式是通过SPSiteDataQuery 类. 使用SPSiteDataQuery 类执行query能够从整个站点集的多个不同的列表中返回列表项. 基于这个原因, 在跨站点查询一般指的就是使用SPSiteDataQuery 的查询.
正如你在最后的例子中见到的, 使用SPQuery对象的查询返回一个SPListItemCollection. 使用SPSiteDataQuery 的查询就不一样了, 因为他们返回的是一个ADO.NET的DataTable对象. 与SPQuery一样, 在DataTable中也可以以field的形式返回columns. 比如说, 假设一个场景, 你运行一个查询, 查询站点集中的每一个基于Announcements 类型的列表, 要求返回所有今天创建的Items. 下面的代码示例会展示如何通过创建一个SPSiteDataQuery 对象, 初始化必要的CAML语句, 然后传递他们给当前SPWeb对象的来GetSiteData 方法, 来满足我们的需求,
//A "Recently Published" Feed
using System;
using System.Web;
using Microsoft.SharePoint;
using System.Data;
using System.Xml;
using Microsoft.SharePoint.Utilities;
namespace Litware.ContentWebParts.Handlers
{
public class RecentPostsHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
SPWeb web = SPContext.Current.Web;
SPSiteDataQuery query = new SPSiteDataQuery();
query.ViewFields =
@"<FieldRef Name=""Title""/><FieldRef Name=""PostCategory""/>
<FieldRef Name=""PublishedDate""/><FieldRef Name=""Body""/>
<FieldRef Name=""Author""/><FieldRef Name=""Permalink""/>
<FieldRef Name=""ContentType""/>";
string queryText =
@"<Where>
<And>
<Eq>
<FieldRef Name=""ContentType"" />
<Value Type=""Text"">Post</Value>
</Eq>
<Eq>
<FieldRef Name=""PublishedDate"" />
<Value Type=""DateTime""><Today /></Value>
</Eq>
</And>
</Where>";
query.Query = queryText;
query.Webs = @"<Webs Scope='Recursive' />";
DataTable table = web.GetSiteData(query);
context.Response.ContentType = "text/xml";
XmlTextWriter xw = new XmlTextWriter(context.Response.Output);
xw.WriteStartElement("rss");
xw.WriteAttributeString("version", "2.0");
xw.WriteStartElement("channel");
xw.WriteElementString("title", "Recently Published: " + web.Title);
xw.WriteElementString("description",
"Recently published posts from " + web.Url);
xw.WriteElementString("link", web.Url);
foreach (DataRow row in table.Rows)
{
xw.WriteStartElement("item");
xw.WriteElementString("title", (string)row["Title"]);
xw.WriteElementString("description", ((string)row["Body"]));
xw.WriteElementString("pubDate", row["PublishedDate"].ToString("r"));
string author =
row["Author"].ToString().Split(new string[] { ";#" },
StringSplitOptions.None)[1];
xw.WriteElementString("author", author);
string category =
row["PostCategory"].ToString().Split(new string[] { ";#" },
StringSplitOptions.None)[1];
xw.WriteElementString("category", category);
string link = string.Format(@"/Lists/Posts/Post.aspx?ID={0}",
row["Permalink"].ToString());
xw.WriteElementString("link", link);
xw.WriteEndElement(); //item
}
xw.WriteEndElement(); //channel
xw.WriteEndElement(); //rss
}
}
}
摘译自:
Inside WSS 3.0 第六章



浙公网安备 33010602011771号