SharePoint 2010开发实例精选——“每日一句”WebPart
?本例將在SharePoint 2010站點上構建一個每日一句Web部件。如果你的電腦上還沒有裝SharePoint 2010環境,可以先從配置 SharePoint 2010開發環境開始做起。為了能夠開發該webpart,你除了需要SharePoint 2010外,還需要VisualStudio 2010。
下圖是最終完成時的效果。它會每天隨機從列表里獲取數據。?
步驟
新建一個visual web part,命名為TOTD。
添加你希望將webpart部署到的站點的URL 。
點擊完成并刪除默認創建的webpart。然后新加一個visual web part,如下圖所示:?
在設計界面上放置一個Image Box,兩個lable,然后根據需要調整表格的布局。?
?HTML代碼
<table?style="width:?409px">????<tr>
????????<td?rowspan="2"?width="100px">
????????????<asp:Image?ID="ImgAuthor"?runat="server"?Width="100px"?Height="100px"?/>
????????</td>
????????<td?style="?height:16px"?valign="top">
????????????<asp:Label?ID="lblTOTD"?runat="server"?Font-Italic="True"?Font-Names="Calibri"?
????????????????Font-Size="12pt"?style="z-index:?1;?left:?120px;?top:?29px;?width:?376px"?
????????????????ForeColor="#003399"></asp:Label>
????????</td>
????</tr>
????<tr>
????????<td?align="right"?valign="top">
????????????<asp:Label?ID="lblAuthor"?runat="server"??Font-Names="Calibri"?Font-Size="9pt"?
????????????????style="z-index:?1;?left:?239px;?top:?97px;?text-align:right;?height:?13px;?width:?252px"></asp:Label>
????????</td>
????</tr>
</table>
控件命名為,ImageBox:ImgAuthor,Lable:lblTOTD,lblAuthor。現在你得到類似如下的設計界面。?
?
OK。現在你已經完成了部件的設計工作,開始編寫后臺代碼。
C#代碼using?System;using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?Microsoft.SharePoint;
using?Microsoft.SharePoint.Utilities;
namespace?TOTD.TOTD_Web_Part
{
????public?partial?class?TOTD_Web_PartUserControl?:?UserControl
????{
????????protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????SPWeb?ospweb?=?SPContext.Current.Web;
????????????SPList?oList?=?ospweb.Lists["QOTD"];
????????????SPListItemCollection?collItem?=?oList.GetItems("Thought",?"AuthorImage",?"AuthorName");
????????????Random?random?=?new?Random();
????????????int?RndItem?=?random.Next(1,?collItem.Count?+?1);
????????????int?LastDay?=?0;
????????????int?TOTD?=?0;
????????????int?CurrentDay?=?DateTime.Now.DayOfYear;
????????????try
????????????{
????????????????LastDay?=?int.Parse(Application["LastDay"].ToString());
????????????????TOTD?=?int.Parse(Application["TOTD"].ToString());
????????????????if?(LastDay?!=?CurrentDay)
????????????????{
????????????????????Application["LastDay"]?=?CurrentDay;
????????????????????Application["TOTD"]?=?RndItem;
????????????????????SPListItem?oItem?=?collItem[RndItem?-?1];
????????????????????this.ImgAuthor.ImageUrl?=?SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?',?'.',?',',?'?'));
????????????????????this.lblTOTD.Text?=?oItem["Thought"].ToString();
????????????????????this.lblAuthor.Text?=?SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
????????????????}
????????????????else
????????????????{
????????????????????SPListItem?oItem?=?collItem[TOTD?-?1];
????????????????????this.ImgAuthor.ImageUrl?=?SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?',?'.',?',',?'?'));
????????????????????this.lblTOTD.Text?=?oItem["Thought"].ToString();
????????????????????this.lblAuthor.Text?=?SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
????????????????}
????????????}
????????????catch
????????????{
????????????????Application["LastDay"]?=?CurrentDay;
????????????????Application["TOTD"]?=?RndItem;
????????????????SPListItem?oItem?=?collItem[RndItem?-?1];
????????????????this.ImgAuthor.ImageUrl?=?SPEncode.HtmlEncode(oItem["AuthorImage"].ToString().TrimEnd('?',?'.',?',',?'?'));
????????????????this.lblTOTD.Text?=?oItem["Thought"].ToString();
????????????????this.lblAuthor.Text?=?SPEncode.HtmlEncode(oItem["AuthorName"].ToString());
????????????}
????????}
????}
}
?
現在,你完成了整個webpart的開發。讓我們來把webpart部署到SharePoint 2010服務器。
在部署前,你需要先在站點里創建一個圖片庫和一個自定義列表。 創建一個名為QOTD的自定義列表,并添加下列的欄:
1、類型->圖片或超鏈接 ;名稱->AuthorImage ;格式化URL為->圖片
2、?類型->單行文本 ;名稱->AuthorName
3、類型->單行文本?;名稱->Thought
然后,將默認的標題字段設置為不必需填寫:?
轉到列表視圖,修改視圖使其只顯示我們需要的三個字段:?
接下來創建圖片庫,以便存放作者的相片。
創建一個名為QOTDImage的圖片庫
創建一個單行文本字段,命名為Author。重命名標題字段為"Name"。最終,你得到類似如下的圖片庫:?
現在,你可以盡可能多的往QOTD列表填數據了。在AuthorImage一欄鏈接到圖片庫中圖片的地址。
這里為“http://ca5-sd-c-022:8080/sites/behive/QOTDImage/Einstine.bmp”。?
準備工作完成。
編譯該項目并點擊部署。然后你就可以再你的網站上使用你的webpart了。
網站操作->編輯頁面->插入(位于編輯功能區)->Web部件->類別->Custom,這樣你就可以找到名為"TOTD_Web_Part"的webpart了。快快添加吧!
?
參考資料
Thought of the day Web part
轉載于:https://www.cnblogs.com/Sunmoonfire/archive/2010/06/23/1763080.html
總結
以上是生活随笔為你收集整理的SharePoint 2010开发实例精选——“每日一句”WebPart的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 调用Remoting的两种激活方式和以及
- 下一篇: Remoting 技术