以下为代码记录,以便不时之需查阅。
功能主要是把sharepoint里面的Discussions模块用代码实现! 当然不用代码直接在Sharepoint操作也可。按需选择!
代码实现了:
Create Discussion Board
Create a new discussion topic
Reply a topic [reply没有做好,不可针对reply回复,只可对topic回复]
DiscussionControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DiscussionControl.ascx.cs" Inherits="MyTestProject.sharepoint.list.DiscussionControl" %> <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <div> <%--board--%> <asp:GridView ID="gvAllDiscussions" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"> <RowStyle ForeColor="#000066" /> <Columns> <asp:TemplateField HeaderText="Title"> <ItemTemplate> <a href='DiscussionPage.aspx?DiscussionGuid=<%# Eval("DiscussionGuid") %>'> <%# Eval("Name") %></a> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Description" DataField="Conent" /> <asp:BoundField HeaderText="Created" DataField="Created" /> <asp:BoundField HeaderText="items" DataField="Items" /> <asp:BoundField HeaderText="Created Time" DataField="CreatedTime" /> <asp:BoundField HeaderText="LastModify" DataField="LastModified" /> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <%--Create--%> Discussion Board Name:<asp:TextBox ID="tbBoardName" runat="server" MaxLength="255" Width="400px"></asp:TextBox><br /> <asp:Label ID="lblError" runat="server" Text=""></asp:Label> <br /> <asp:Button ID="BtnCreateBoard" runat="server" Text="CreateDiscussionBoard" onclick="BtnCreateBoard_Click" /> <hr /> <%--End Board--%> <h3>点击上面DiscusionBoard显示相应的Topic</h3> <!--Topic--> <asp:GridView ID="gvTopic" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"> <RowStyle ForeColor="#000066" /> <Columns> <asp:TemplateField HeaderText="Title"> <ItemTemplate> <a href='DiscussionPage.aspx?DiscussionGuid=<%# Eval("DiscussionGuid") %>&TopicId=<%# Eval("Id") %>'> <%# Eval("Name") %></a> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Conent" DataField="Conent" /> <asp:BoundField HeaderText="Created" DataField="Created" /> <asp:BoundField HeaderText="Replies" DataField="Items" /> <asp:BoundField HeaderText="LastModify" DataField="LastModified" /> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <%--Create--%> Subject:<asp:TextBox ID="tbTopicTitle" runat="server" Width="270px" MaxLength="255"></asp:TextBox><br /> Body:<asp:TextBox ID="tbTopicBody" runat="server" Width="258px"></asp:TextBox><br /> <asp:Button ID="BtnCreateTopic" runat="server" Text="CreateTopic" onclick="BtnCreateTopic_Click" /> <%--End Topic--%> <hr /> <%--Reply--%> <h3>点击上面的Topic,显示相应的Reply信息</h3> <asp:GridView ID="gvReplies" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"> <RowStyle ForeColor="#000066" /> <Columns> <asp:TemplateField HeaderText="Title"> <ItemTemplate> <a href='DiscussionPage.aspx?DiscussionGuid=<%# Eval("DiscussionGuid") %>&TopicId=<%# Eval("Id") %>'> <%# Eval("Name") %></a> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Conent" DataField="Conent" /> <asp:BoundField HeaderText="Created" DataField="Created" /> <asp:BoundField HeaderText="Replies" DataField="Items" /> <asp:BoundField HeaderText="CreatedTime" DataField="CreatedTime" /> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <%--Create--%> Reply Body:<asp:TextBox ID="tbReply" runat="server" Height="44px" Width="375px" /><br /> <asp:Button ID="Btnreply" runat="server" Text="reply" onclick="Btnreply_Click" /> <%--End Reply--%> </div>
DiscussionControl.ascx.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; namespace MyTestProject.sharepoint.list { public partial class DiscussionControl : System.Web.UI.UserControl { private const string siteUrl = "http://SharepointServer:5000"; protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; //绑定Discussion board gvAllDiscussions.DataSource = GetAllDiscussions(); gvAllDiscussions.DataBind(); string discussionGuid = Request.QueryString["discussionGuid"]; string topicId = Request.QueryString["TopicId"]; //绑定topic if (!string.IsNullOrEmpty(discussionGuid)) { BindTopic(discussionGuid); } //绑定回复 if(!string.IsNullOrEmpty(topicId)) { BindReplies(discussionGuid, topicId); } } /// <summary> /// 获取回复 /// </summary> /// <param name="discussionGuid"></param> /// <param name="topicId"></param> private void BindReplies(string discussionGuid, string topicId) { List<Discussion> discussionReplies = new List<Discussion>(); using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.RootWeb) { SPList list = web.Lists[new Guid(discussionGuid)]; SPListItemCollection items = list.Items; Response.Write("items:" + items.Count); foreach (SPListItem item in items) { //判断是否是该帖子的回复信息 if (item["ParentFolderId"] + "" == topicId) { Discussion discussion = new Discussion { Id = item.ID, Created = item.Fields["Created By"].GetFieldValueAsText( item["Created By"]), DiscussionGuid = list.ID, Name = item.DisplayName, Items = item.Fields["Replies"].GetFieldValueAsHtml(item["Replies"]), Conent = item.Fields["Body"].GetFieldValueAsText(item["Body"]) }; discussion.CreatedTime = Convert.ToDateTime(item["Created"]); discussionReplies.Add(discussion); } } gvReplies.DataSource = discussionReplies; gvReplies.DataBind(); } } /// <summary> /// 获取帖子 /// </summary> /// <param name="discussionGuid"></param> private void BindTopic(string discussionGuid) { Guid boardGuid = new Guid(discussionGuid); SPQuery query = new SPQuery { RowLimit = 5,//获取5个 Query = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"False\" /></OrderBy>" }; List<Discussion> discussionTopics = new List<Discussion>(); using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.RootWeb) { SPList list = web.Lists[boardGuid]; //SPListItemCollection folders = list.GetItems(query); /** * 注意:获取帖子 为list.Folders; 而不是 list.Items * * */ SPListItemCollection folders = list.Folders; foreach (SPListItem folder in folders) { discussionTopics.Add(new Discussion() { Id = folder.ID, Created = folder.Fields["Created By"].GetFieldValueAsText( folder["Created By"]), DiscussionGuid = list.ID, Name = folder.Title, Items = folder.Fields["Replies"].GetFieldValueAsHtml(folder["Replies"]), Conent = folder.Fields["Body"].GetFieldValueAsText(folder["Body"]), //CreatedTime = list.Created.ToLocalTime(), LastModified = Convert.ToDateTime(folder["Last Updated"].ToString()).ToLocalTime() }); } } gvTopic.DataSource = discussionTopics; gvTopic.DataBind(); } /// <summary> /// 获取当前web的所有 DiscussionBoard /// </summary> /// <returns></returns> public List<Discussion> GetAllDiscussions() { List<Discussion> discussions = new List<Discussion>(); using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.RootWeb) { //SPBaseType.DiscussionBoard 怎么这个不行? [比web.lists要少几次循环] SPListCollection lists = web.GetListsOfType(SPBaseType.GenericList); Response.Write("DiscussionBoard" + lists.Count + " ### " + web.Lists.Count); foreach (SPList list in lists) { //取出时 DiscussionBoard 的splist if (list.BaseTemplate == SPListTemplateType.DiscussionBoard && list.Hidden == false) { discussions.Add(new Discussion() { Created = list.Author.Name, DiscussionGuid = list.ID, Name = list.Title, Items = (list.ItemCount - list.Items.Count)+"", Conent = list.Description, CreatedTime = list.Created.ToLocalTime(), LastModified = list.LastItemModifiedDate.ToLocalTime(), }); } } return discussions; } } public class Discussion : IComparable { public int Id { get; set; } public Guid DiscussionGuid { get; set; } public string Name { get; set; } public string Items { get; set; } public DateTime LastModified { get; set; } public string Created { get; set; } public DateTime CreatedTime { get; set; } public string Conent { get; set; } public Discussion() { } #region IComparable Members public int CompareTo(object obj) { return (this.Items).Items.CompareTo((Discussion)obj); } #endregion } /// <summary> /// 创建Board /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnCreateBoard_Click(object sender, EventArgs e) { //注意 textbox已设置:MaxLength="255" sharepoint里面默认255 string name = tbBoardName.Text.Trim(); using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.RootWeb) { SPListCollection lists = web.Lists; try { SPList disBoard = web.Lists[name]; lblError.Text = "The name that you used already exist. If you receive this in error, please contact your administrator!"; return; } catch (Exception) { web.AllowUnsafeUpdates = true; //设置允许不安全的更改 //创建列表 Guid guidDiscussionBoard = lists.Add(name, name, SPListTemplateType.DiscussionBoard); SPList list = web.Lists[guidDiscussionBoard]; list.OnQuickLaunch = true;//设置一些属性 web.Update(); } } Response.Redirect(Request.Url.OriginalString); } /// <summary> /// 创建帖子 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnCreateTopic_Click(object sender, EventArgs e) { string title = tbTopicTitle.Text.Trim(); string body = tbTopicBody.Text.Trim(); Guid boardGuid = new Guid(Request.QueryString["discussionGuid"]); using (SPSite site = new SPSite(siteUrl)) { using (SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true; SPList list = web.Lists[boardGuid]; //CreateNewDiscussion调用该方法即可 SPListItem newItem = SPUtility.CreateNewDiscussion(list.Items, title); newItem["Body"] = body; newItem.Update(); } } Response.Redirect(Request.Url.OriginalString); } /// <summary> /// 回复帖子 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Btnreply_Click(object sender, EventArgs e) { Guid boardGuid = new Guid(Request.QueryString["discussionGuid"]); string topicId = Request.QueryString["TopicId"]; using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists[boardGuid]; //获取帖子 SPListItem itemTopic = list.GetItemById(Convert.ToInt32(topicId)); //在该帖子里面创建回复 SPListItem itemTopicReply = SPUtility.CreateNewDiscussionReply(itemTopic); itemTopicReply["Body"] = tbReply.Text.Trim(); web.AllowUnsafeUpdates = true; site.AllowUnsafeUpdates = true; itemTopicReply.Update(); } Response.Redirect(Request.Url.OriginalString); } /** * error:Operation is not valid due to the current state of the object. [操作无效由于对象的当前状态] * * */ } }
如何运行:
如果你只在VS中运行,只需在新建一个page放置上面的userControl即可。
如果你想配置到Sharepoint上面运行,则需另做一些操作:
1.把DiscussionControl 拷贝到 C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\sptest 目录下面, sptest 是我新建,你可以随意取名字[不是中文最好]
2.用SPD打开你的站点(siteUrl) :在根目录下面新建一个aspx页面,如DiscussionPage.aspx ,打开把下面代码覆盖进去 [sptest修改为你的目录]
<%@ Page Language="C#" %> <%@ Register src="~/_controltemplates/sptest/DiscussionControl.ascx" tagname="DiscussionControl" tagprefix="uc1" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <META name="WebPartPageExpansion" content="full"> <title></title> </head> <body> <form id="form1" runat="server"> <uc1:DiscussionControl ID="DiscussionControl1" runat="server"/> </form> </body> </html>
3.把上面项目生成的DLL文件拷贝到 C:\inetpub\wwwroot\wss\VirtualDirectories\5000\bin 目录下面! 5000为你的siteUrl地址里面的端口
4.在IE地址栏输入:http://SharepointServer:5000/DiscussionPage.aspx 即可看到效果了!
其他:
如果没有Dll,把后台cs文件写在页面上,用<script runat=”sever”>code…</script> 那样可以直接把页面拷贝到
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\layouts
目录下, http://SharepointServer:5000/_layouts/DiscussionPage.aspx 即可!
效果图: