需要EditPart.列出两个类。这里以滚动图片为例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
using System.Diagnostics;
using System.Xml;
namespace ScrollPictureWebPart
{
public class ScrollPicture : System.Web.UI.WebControls.WebParts.WebPart
{
private string _siteurl = string.Empty;
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("网站地址")]
//[WebDescription("SiteUrl Description")]
public string SiteUrl
{
get { return _siteurl; }
set { _siteurl = value; }
}
private string _webname = string.Empty;
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("web名称")]
//[WebDescription("WebUrl Description")]
public string WebName
{
get { return _webname; }
set { _webname = value; }
}
//[Personalizable]
//[WebBrowsable(false)]
//[WebDescription("是否文档库")]
//[WebDisplayName("是否文档库")]
public bool IsDoc
{
get;
set;
}
private string _listname = string.Empty;
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("图片库名称")]
//[WebDescription("ListName Description")]
public string ListName
{
get { return _listname; }
set { _listname = value; }
}
private string _scrollwidth = "610";
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("滚动宽度")]
//[WebDescription("ListName Description")]
public string ScrollWidth
{
get { return _scrollwidth; }
set { _scrollwidth = value; }
}
private string _recordcount = "6";
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("图片个数")]
//[WebDescription("RecordCount Description")]
public string RecordCount
{
get { return _recordcount; }
set { _recordcount = value; }
}
private string _imgwidth = "149";
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("图片宽度")]
//[WebDescription("ImageWidth Description")]
public string ImageWidth
{
get { return _imgwidth; }
set { _imgwidth = value; }
}
private string _imgheight = "100";
//[Personalizable]
//[WebBrowsable]
//[WebDisplayName("图片高度")]
//[WebDescription("ImageHeight Description")]
public string ImageHeight
{
get { return _imgheight; }
set { _imgheight = value; }
}
protected override void CreateChildControls()
{
//base.CreateChildControls();
this.Controls.Clear();
}
/// <summary>
/// 创建编辑控件
/// </summary>
/// <returns></returns>
public override EditorPartCollection CreateEditorParts()
{
EditorPartCollection baseParts = base.CreateEditorParts();
List<EditorPart> editorParts = new List<EditorPart>(1);
EditorPart part = new ScrollPictureEditPart();
try
{
part.ID = this.ID + "_ValueEditor";
part.Title = "滚动图片";
editorParts.Add(part);
}
catch (Exception ex)
{
WriteLog(ex.ToString());
}
return new EditorPartCollection(baseParts, editorParts);
}
public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
{
try
{
base.EnsureChildControls();
DataTable dt = GetDataTable();
if (dt != null && dt.Rows.Count > 0)
{
writer.Write("<table cellspacing='0' cellpadding='0' width=" + this.ScrollWidth + " align='center' bgcolor='#ffffff' border='0'>");
writer.Write("<tbody>");
writer.Write("<tr>");
writer.Write("<td bgcolor='#ffffff' height='1'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td height='1'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td colspan='2'>");
writer.Write("<div id='demom' style='overflow: hidden; width: " + this.ScrollWidth + "'>");
writer.Write("<table bordercolor='#ffffff' cellspacing='0' cellpadding='0' width=" + this.ScrollWidth + " align='center' border='0' cellspace='0'>");
writer.Write("<tbody>");
writer.Write("<tr>");
writer.Write("<td id='demom1' valign='center'>");
writer.Write("<table bordercolor='#ffffff' height='104' cellspacing='0' cellpadding='0' align='center' border='0'>");
writer.Write("<tbody>");
writer.Write("<tr>");
//图片插入
for (int i = 0; i < dt.Rows.Count; i++)
{
writer.Write("<td valign='bottom' nowrap align='middle'>");
writer.Write("<table cellspacing='1' cellpadding='1' bgcolor='#569d95' border='0'>");
writer.Write("<tbody>");
writer.Write("<tr>");
writer.Write("<td bgcolor='#ffffff'>");
writer.Write("<a href=" + dt.Rows[i]["ItemUrl"].ToString() + " target='_blank'>");
writer.Write("<img height=" + this.ImageHeight + " src=" + dt.Rows[i]["Url"].ToString() + " width=" + this.ImageWidth + " border='0'></a>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</tbody>");
writer.Write("</table>");
writer.Write("</td>");
writer.Write("<td valign='bottom' nowrap align='middle' width='3'>");
writer.Write("</td>");
}
writer.Write("</tr>");
writer.Write("</tbody>");
writer.Write("</table>");
writer.Write("</td>");
writer.Write("<td id='demom2' valign='top'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</tbody>");
writer.Write("</table>");
writer.Write("</div>");
writer.Write("<script language='javascript'>\n\r");
writer.Write("var speed=15\n\r");
writer.Write("demom2.innerHTML=demom1.innerHTML\n\r");
writer.Write("function Marquee1(){\n\r");
writer.Write("if(demom2.offsetWidth-demom.scrollLeft<=0)\n\r");
writer.Write("demom.scrollLeft-=demom1.offsetWidth\n\r");
writer.Write("else{\n\r");
writer.Write("demom.scrollLeft++\n\r");
writer.Write("}\n\r");
writer.Write("}\n\r");
writer.Write("var MyMar1=setInterval(Marquee1,speed)\n\r");
writer.Write("demom.onmouseover=function() {clearInterval(MyMar1)}\n\r");
writer.Write("demom.onmouseout=function() {MyMar1=setInterval(Marquee1,speed)}\n\r");
writer.Write("</script>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</tbody>");
writer.Write("</table>");
}
dt.Dispose();
}
catch (Exception ex)
{
WriteLog(ex.ToString());
}
}
public System.Data.DataTable GetDataTable()
{
System.Data.DataTable dt = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite mySite = new SPSite(this.SiteUrl))
{
//SPWeb webs = mySite.OpenWeb(this.WebName);
SPWeb webs = GetWeb(mySite, this.WebName);
SPDocumentLibrary docLib = (SPDocumentLibrary)webs.Lists[this.ListName];
dt = GetListItem(docLib, Convert.ToInt32(this.RecordCount));
}
}
);
}
catch (Exception ex)
{
WriteLog(ex.ToString());
}
return dt;
}
/// <summary>
/// 获取文档库数据
/// </summary>
/// <returns>返回DataTable数据</returns>
public DataTable GetListItem(SPDocumentLibrary docLib, int recordcount)
{
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
//query.Query = "<OrderBy><FieldRef Name=\"CreateTime\" Ascending=\"FALSE\"/></OrderBy>";
//query.Query = "<Where><Eq><FieldRef Name=\"Type\"></FieldRef><Value Type=\"Text\">" + strType + "</Value></Eq></Where><OrderBy><FieldRef Name=\"Created\" Ascending=\"FALSE\"/></OrderBy>";
SPListItemCollection listItems = docLib.GetItems(query);
DataTable table = new DataTable();
if (listItems.Count != 0)
{
table.Columns.Add("ItemUrl");
table.Columns.Add("Url");
int count = 1;
foreach (SPListItem item in listItems)
{
DataRow row = table.NewRow();
row["Url"] = docLib.ParentWeb.Url.ToString() + "/" + item.Url.ToString();
row["ItemUrl"] = docLib.DefaultViewUrl.Substring(0, docLib.DefaultViewUrl.Length - 13) + "DispForm.aspx?ID=" + item.ID.ToString();
table.Rows.Add(row);
if (count >= recordcount)
{
break;
}
count++;
}
}
return table;
}
/// <summary>
/// 根据网站标题获取网站对象
/// </summary>
/// <param name="site">SPSite对象</param>
/// <param name="strWebTitle">网站标题</param>
/// <returns>返回网站对象</returns>
public SPWeb GetWeb(SPSite site, string strWebTitle)
{
try
{
SPWeb spweb = null;
foreach (SPWeb web in site.AllWebs)
{
if (web.Title == strWebTitle)
{
spweb = web;
web.Close();
web.Dispose();
break;
}
}
return spweb;
}
catch
{
return null;
}
}
#region Write System Log
public void WriteLog(string ex)
{
if (!EventLog.SourceExists("News"))
{
EventLog.CreateEventSource("News", "NewsLog");
}
EventLog.WriteEntry("News", ex.ToString(), EventLogEntryType.Error);
}
#endregion
}
}
2。EditPart:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
namespace ScrollPictureWebPart
{
public class ScrollPictureEditPart : EditorPart
{
//站点
protected DropDownList ddlSite;
//网站
protected DropDownList ddlWebList;
//列表
protected DropDownList ddlList;
//是否文档库
protected DropDownList ddlListType;
//滚动宽度
protected TextBox tbScrollWidth;
////滚动方向
//protected TextBox tbScrollDirection;
////滚动速度
//protected TextBox tbScrollSpeed;
//图片个数
protected TextBox tbRecord;
//图片高度
protected TextBox tbPictureHeight;
//图片宽度
protected TextBox tbPictureWidth;
/// <summary>
/// 创建控件
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();
ScrollPicture webpart = this.WebPartToEdit as ScrollPicture;
this.ddlSite = new DropDownList();
this.ddlSite.ID = "ddlSite";
this.ddlSite.AutoPostBack = true;
this.ddlSite.SelectedIndexChanged += new EventHandler(ddlSite_SelectedIndexChanged);
this.ddlWebList = new DropDownList();
this.ddlWebList.ID = "ddlWebList";
this.ddlWebList.AutoPostBack = true;
this.ddlWebList.SelectedIndexChanged += new EventHandler(ddlWebList_SelectedIndexChanged);
this.ddlListType = new DropDownList();
this.ddlListType.ID = "ddlListType";
this.ddlListType.AutoPostBack = true;
this.ddlListType.SelectedIndexChanged += new EventHandler(ddlListType_SelectedIndexChanged);
this.ddlListType.Items.Clear();
this.ddlListType.Items.Add(new ListItem("列表", "0"));
this.ddlListType.Items.Add(new ListItem("文档库", "1"));
this.ddlList = new DropDownList();
this.ddlList.ID = "ddlList";
tbScrollWidth = new TextBox();
tbScrollWidth.ID = "tbScrollWidth";
tbScrollWidth.Text = "610";
//tbScrollDirection = new TextBox();
//tbScrollDirection.ID = "tbScrollDirection";
//tbScrollWidth.Text = "610";
//tbScrollSpeed = new TextBox();
//tbScrollSpeed.ID = "tbScrollSpeed";
//tbScrollSpeed.Text = "15";
tbRecord = new TextBox();
tbRecord.ID = "tbRecord";
tbRecord.Text = "6";
tbPictureHeight = new TextBox();
tbPictureHeight.ID = "tbPictureHeight";
tbPictureHeight.Text = "100";
tbPictureWidth = new TextBox();
tbPictureWidth.ID = "tbPictureWidth";
tbPictureWidth.Text = "149";
Label label = new Label();
label.ID = "label";
label.Text = "滚动图片:";
// 表格控件(布局)
Table table = new Table();
table.CellPadding = 2;
table.CellSpacing = 1;
table.Style.Add(HtmlTextWriterStyle.Width, "300");
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Text = "选择站点:";
cell2.Controls.Add(this.ddlSite);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "选择网站:";
cell2.Controls.Add(this.ddlWebList);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "选择列表类型";
cell2.Controls.Add(this.ddlListType);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "选择列表:";
cell2.Controls.Add(this.ddlList);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "滚动宽度:";
cell2.Controls.Add(this.tbScrollWidth);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "图片个数:";
cell2.Controls.Add(this.tbRecord);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "图片高度:";
cell2.Controls.Add(this.tbPictureHeight);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
row = new TableRow();
cell1 = new TableCell();
cell2 = new TableCell();
cell1.Text = "图片宽度:";
cell2.Controls.Add(this.tbPictureWidth);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
this.Controls.Add(table);
try
{
this.ddlSite.Items.Clear();
try
{
// 枚举网站
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService webService in webServices)
{
foreach (SPWebApplication webApp in webService.WebApplications)
{
foreach (SPSite site in webApp.Sites)
{
if (site.Url != null && site.Url != string.Empty)
{
this.ddlSite.Items.Add(site.Url);
}
}
}
}
}
catch
{
this.ddlSite.Items.Add(SPContext.Current.Site.Url);
}
ListItem item;
if (string.IsNullOrEmpty(webpart.SiteUrl))
{
item = this.ddlSite.Items.FindByValue(SPContext.Current.Site.Url);
if (item != null)
{
item.Selected = true;
}
}
else
{
item = this.ddlSite.Items.FindByValue(webpart.SiteUrl);
if (item != null)
{
item.Selected = true;
}
}
this.BindWeb();
if (string.IsNullOrEmpty(webpart.WebName))
{
item = this.ddlWebList.Items.FindByText(SPContext.Current.Site.RootWeb.Title);
if (item != null)
{
item.Selected = true;
}
}
else
{
item = this.ddlWebList.Items.FindByText(webpart.WebName);
if (item != null)
{
item.Selected = true;
}
}
if (!string.IsNullOrEmpty(webpart.ListName))
{
//item = this.ddlList.Items.FindByValue("0");
//if (item != null)
//{
// item.Selected = true;
//}
item = this.ddlList.Items.FindByValue(webpart.ListName);
if (item != null)
{
item.Selected = true;
}
}
//else
//{
// item = this.ddlList.Items.FindByValue(webpart.ListName);
// if (item != null)
// {
// item.Selected = true;
// }
//}
//设置列表类型
if (webpart.IsDoc)
{
this.ddlListType.SelectedIndex = 1;
}
else
{
this.ddlListType.SelectedIndex = 0;
}
this.BindListDoc();
if (string.IsNullOrEmpty(webpart.ScrollWidth))
{
this.tbScrollWidth.Text = "610";
}
else
{
this.tbScrollWidth.Text = webpart.ScrollWidth;
}
if (string.IsNullOrEmpty(webpart.RecordCount))
{
this.tbRecord.Text = "6";
}
else
{
this.tbRecord.Text = webpart.RecordCount;
}
if (string.IsNullOrEmpty(webpart.ImageHeight))
{
this.tbPictureHeight.Text = "100";
}
else
{
this.tbPictureHeight.Text = webpart.ImageHeight;
}
if (string.IsNullOrEmpty(webpart.ImageWidth))
{
this.tbPictureWidth.Text = "149";
}
else
{
this.tbPictureWidth.Text = webpart.ImageWidth;
}
}
catch (Exception ex)
{
WriteLog(ex.ToString());
}
}
/// <summary>
/// 列表类型改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ddlListType_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindListDoc();
//this.BindView();
}
///// <summary>
///// 列表改变事件
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//public void ddlList_SelectedIndexChanged(object sender, EventArgs e)
//{
// this.BindView();
//}
/// <summary>
/// 网站改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ddlWebList_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindListDoc();
//this.BindView();
}
/// <summary>
/// 站点改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ddlSite_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindWeb();
this.ddlWebList.SelectedIndex = 1;
this.BindListDoc();
//this.BindView();
}
/// <summary>
/// 保存设置。
/// </summary>
/// <returns></returns>
public override bool ApplyChanges()
{
ScrollPicture webpart = this.WebPartToEdit as ScrollPicture;
if (this.ddlListType.SelectedValue == "0")
{
webpart.IsDoc = false;
}
else
{
webpart.IsDoc = true;
}
webpart.ListName = this.ddlList.SelectedValue;
if (this.ddlSite.SelectedItem.Value != "-1")
{
webpart.SiteUrl = this.ddlSite.SelectedItem.Value;
}
if (this.ddlWebList.SelectedItem.Value != "-1")
{
webpart.WebName = this.ddlWebList.SelectedItem.Value;
}
if (this.ddlList.SelectedItem.Value != "")
{
webpart.ListName = this.ddlList.SelectedItem.Value;
}
if (!string.IsNullOrEmpty(this.tbScrollWidth.Text.ToString()))
{
webpart.ScrollWidth = this.tbScrollWidth.Text.ToString();
}
if (!string.IsNullOrEmpty(this.tbPictureWidth.Text.ToString()))
{
webpart.ImageWidth = this.tbPictureWidth.Text.ToString();
}
if (!string.IsNullOrEmpty(this.tbPictureHeight.Text.ToString()))
{
webpart.ImageHeight = this.tbPictureHeight.Text.ToString();
}
if (!string.IsNullOrEmpty(this.tbRecord.Text.ToString()))
{
webpart.RecordCount = this.tbRecord.Text.ToString();
}
return true;
}
/// <summary>
/// 初始化。
/// </summary>
public override void SyncChanges()
{
}
/// <summary>
/// 绑定网站
/// </summary>
public void BindWeb()
{
try
{
this.ddlWebList.Items.Clear();
this.ddlWebList.Items.Add(new ListItem("选择网站", "-1"));
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite spSite = new SPSite(this.ddlSite.SelectedItem.Value);
foreach (SPWeb web in spSite.AllWebs)
{
if (web.Url != null && web.Url != string.Empty)
{
this.ddlWebList.Items.Add(web.Title);
}
}
spSite.Close();
spSite.Dispose();
}
);
}
catch (Exception ex)
{
this.Page.Response.Write(ex.Message);
}
}
/// <summary>
/// 绑定文档库、列表库
/// </summary>
public void BindListDoc()
{
try
{
ScrollPicture webpart = this.WebPartToEdit as ScrollPicture;
this.ddlList.Items.Clear();
if (this.ddlWebList.SelectedValue == "-1")
{
return;
}
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite spSite = new SPSite(this.ddlSite.SelectedItem.Value);
SPWeb spWeb = GetWeb(spSite, this.ddlWebList.SelectedValue);
foreach (SPList list in spWeb.Lists)
{
if (list.Hidden == false)
{
if (this.ddlListType.SelectedValue == "0" && list.BaseType == SPBaseType.GenericList)
{
this.ddlList.Items.Add(list.Title);
}
else if (this.ddlListType.SelectedValue == "1" && list.BaseType == SPBaseType.DocumentLibrary)
{
this.ddlList.Items.Add(list.Title);
}
}
}
spWeb.Close();
spWeb.Dispose();
spSite.Close();
spSite.Dispose();
});
if (!string.IsNullOrEmpty(webpart.ListName))
{
ListItem item = this.ddlList.Items.FindByValue(webpart.ListName);
if (item != null)
{
item.Selected = true;
}
}
}
catch (Exception ex)
{
this.Page.Response.Write(ex.Message);
}
}
/// <summary>
/// 根据网站标题获取网站对象
/// </summary>
/// <param name="site">SPSite对象</param>
/// <param name="strWebTitle">网站标题</param>
/// <returns>返回网站对象</returns>
public SPWeb GetWeb(SPSite site, string strWebTitle)
{
try
{
SPWeb spweb = null;
foreach (SPWeb web in site.AllWebs)
{
if (web.Title == strWebTitle)
{
spweb = web;
web.Close();
web.Dispose();
break;
}
}
return spweb;
}
catch
{
return null;
}
}
#region Write System Log
public void WriteLog(string ex)
{
if (!EventLog.SourceExists("News"))
{
EventLog.CreateEventSource("News", "NewsLog");
}
EventLog.WriteEntry("News", ex.ToString(), EventLogEntryType.Error);
}
#endregion
}
}