如图:
在图中,我们只看到了 创建者, 最后更新的时间, 等等,但是不知道谁是最后回复的人! 而一般论坛都会显示为: 最后回复 某某 11/16/2010/5:49 . 这里来我们通过代码获取最后回复的人!
方法一: 此方法循环量太大
protected void Page_Load(object sender, EventArgs e) { GetDisBoard(); } public void GetDisBoard() { using (SPSite site = new SPSite("http://moss:5000")) using (SPWeb web = site.RootWeb) { SPList list = web.Lists["Team Discussion"]; //获取帖子 [注意:这里list.Folders才是帖子, 而不是Items] foreach (SPListItem spListItem in list.Folders) { Response.Write(GetLastPost(spListItem.ID)); Response.Write("<p></p>"); } } } public string GetLastPost(int topicID) { List<string> replies = new List<string>(); using (SPSite site = new SPSite("http://moss:5000")) using (SPWeb web = site.RootWeb) { SPList list = web.Lists["Team Discussion"];//Items:包含了 帖子和回复
foreach (SPListItem item in list.Items) { //获取帖子的回复 ParentFolderId指该回复的帖子ID if (item["ParentFolderId"] + "" == topicID + "") { replies.Add("Last reply by " + item.Fields["Created By"].GetFieldValueAsText(item["Created By"]) + " on " + item["Modified"]); } } } string lastReply = "No one reply"; if (replies.Count > 0) { lastReply = replies[replies.Count - 1]; } return lastReply; }
2. 该方法循环次数有所减少,但是SPSiteDataQuery 对象执行起来也比较耗时!
public void GetQuery(string DiscussionLastUpdated) { using (SPSite site = new SPSite("http://moss:5000")) using (SPWeb web = site.RootWeb) { SPSiteDataQuery query = new SPSiteDataQuery(); query.Webs = "<Webs Scope=\"SiteCollection\">"; // 108 表示Discussion http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.lists.aspx query.Lists = "<Lists ServerTemplate=\"108\" />"; //query.ViewFields = // "<FieldRef Name=\"Title\" /><FieldRef Name=\"Created\" /><FieldRef Name=\"DiscussionLastUpdated\" />"; query.ViewFields = "<FieldRef Name=\"Author\" />"; // 中国时间要 –8 这里出来的格式为: 2010-11-17T01:49:01ZDiscussionLastUpdated = SPUtility.FormatDate(web, Convert.ToDateTime(DiscussionLastUpdated).AddHours(-8), SPDateFormat.ISO8601); //DiscussionLastUpdated 为最后回复的时间, 即最后回复的创建时间 query.Query = @"<Where><Eq><FieldRef Name='Created' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + DiscussionLastUpdated + "</Value></Eq></Where>"; DataTable dt = web.GetSiteData(query); foreach (DataRow row in dt.Rows) { Response.Write(row["Author"] + "" + row["Body"] + " Last by :" + row["DiscussionLastUpdated"] + "<br />"); Response.Write("<hr style='color=red'>"); } } } public void GetDisBoard() { using (SPSite site = new SPSite("http://moss:5000")) using (SPWeb web = site.RootWeb) { SPList list = web.Lists["Team Discussion"]; //获取帖子 foreach (SPListItem spListItem in list.Folders) { GetQuery(spListItem["DiscussionLastUpdated"] + ""); } } }期待更好的方法出来! 这个功能也许意义不大, 但是通过代码可以了解到一些对象的使用!
今天在帖子:http://social.microsoft.com/Forums/zh-CN/sharepointwebpartzhchs/thread/afcb3aab-0238-47bd-968a-125cf85dacf3
MVP Erucy 回复了我, 感谢Erucy提供的帮助.
你可以使用SPQuery对象,这个对象有一个Folder属性,会限制查询的范围 当你找某一个主题最新帖子的时候(主题本质上是一个文件夹) 你就可以把这个文件夹作为参数设置到SPQuery上面 然后: query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>"; // 按创建时间逆序排序 query.RowLimit = 1; // 只取最新的一个 然后再list.GetItems(query) 就可以了,如果有回复的话就会取到最新一个回复的item,后面的你应该就会了
code: 已通过测试
using (SPSite site = new SPSite("http://jianghao:5000")) using (SPWeb web = site.RootWeb) { SPList list = web.Lists["Team Discussion"]; //获取帖子 SPQuery query; foreach (SPListItem spListItem in list.Folders) { query = new SPQuery(); query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>"; //倒序 query.RowLimit = 1;//取一个 query.Folder = spListItem.Folder; //关键代码,设置Folder属性 SPListItemCollection replies = list.GetItems(query); Response.Write(replies.Count == 0 ? "No reply <br />" : replies[0].Fields["Created By"].GetFieldValueAsText(replies[0]["Created By"]) + "<br />"); } }
相关文章:Sharepoint中用编码的方式实现Discussions的创建、删除、回复
Technorati 标签: sharepoint,discussion
记录学习点滴...,坚持每天让自己的技能增加1%,默默的坚持下去吧!:-)