MOSS Event Handler实用
打开管理中心,在Application Management中,进入Web Application General Settings,将目标站点下的 Event Handler 控制打开。
实现获取SharePoint 文档列表操作事件:新建一个类库,添加引用Microsoft.SharePoint.dll 在系统盘C:"Program Files"Microsoft Shared"Web Server extensions"12"ISAPI中。该类还需继承IListEvent Sink 的接口,该接口中的OnEvent(SPListEvent list)方法,判断该列表中所执行的事件类型(SPListEventType.Type),然后写上该事件触发后所要执行的操作,如记录操作日志等。事件写好后,编译,将生成的dll Copy到当前需要加载的SharePoint 站点dll文件夹中。
打开文档列表,在文档列表设置中,选择高级设置,将Event Handler 的属性名,也就是dll的文件名,加命名空间下的类名添加进去。这样当你在文件列表中操作触发你所写的事件类型时,将同时执行你所写的操作。下列代码为你删除文件时,记录删除时间!
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
namespace Bright.EventHandler.Demo
{
public class EventHandlerClass1:IListEventSink
{
public void OnEvent(SPListEvent spe)
{
if (spe.Type == SPListEventType.Delete)
{
string path = @"D:\Documents and Settings\Administrator\Desktop\MyEventHandlerTest.txt";
string strText = "My EventHandler Test" + DateTime.Now.ToString();
StreamWriter sw = new StreamWriter(path);
sw.Write(strText);
sw.Close();
}
}
}
}
实现通用的Event Handler:下面讲述一个比较实用的方法,方法可能不是最好的,请大家提意见!
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.SharePoint;
namespace liang.pic.test
{
public class ShowPicClass1:WebPart
{
private HtmlImage mi = new HtmlImage();
private HtmlTableCell htc = new HtmlTableCell();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
CreateChildControls();
}
private void ShowImage()
{
htc.Controls.Add(new LiteralControl("<table>" + "\n"));
htc.Controls.Add(new LiteralControl("<tr>" + "\n"));
htc.Controls.Add(mi);
htc.Controls.Add(new LiteralControl("</tr>" + "\n"));
htc.Controls.Add(new LiteralControl("</table>" + "\n"));
mi.Src = @"C:\Documents and Settings\liangliang\My Documents\Visual Studio 2008\Projects\liang.pic.test\liang.pic.test\pic\Follow.jpg";
}
protected override void CreateChildControls()
{
base.CreateChildControls();
SPSite site = new SPSite("http://mossserver:9000/pic/");
SPWeb web = site.OpenWeb();
//SPList list = web.Lists["pic"];
//site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
//list.Items.Add(@"C:\Autumn.jpg", SPFileSystemObjectType.File);
//SPWeb spw = SPContext.Current.Web;
StreamReader sr = new StreamReader(@"C:\Autumn.jpg");
Stream st = sr.BaseStream;
byte[] bt = new byte[st.Length];
st.Read(bt, 0, (int)st.Length);
st.Close();
web.Files.Add("http://mossserver:9000/pic/chenchao.jpg", bt, true);
this.Controls.Add(new LiteralControl("<table border=1>" + "\n"));
this.Controls.Add(new LiteralControl("<tr>" + "\n"));
this.Controls.Add(htc);
this.Controls.Add(new LiteralControl("</tr>" + "\n"));
this.Controls.Add(new LiteralControl("</table>" + "\n"));
//ShowImage();
}
//private string _text = "this is my webpark test!!!";
//public string test
//{
// get { return _text; }
// set { _text = value; }
//}
//protected override void Render(System.Web.UI.HtmlTextWriter writer)
//{
// writer.Write(_text);
//}
}
}
看代码很烦吧,呵呵。没办法,稍微解释下用途,我做的列子是做一个放图片的webpart,放在文档列表栏,怎么放,前面讲过就不重复了。当该页加载时,会自动将一张图片上传到文档的列表中,不用单独点击上传了。这是个很简单的使用,我们可以引申出更多用法。webpart中可以自定义作很多事,读数据库,解析数据,然后上传个报表。为什么放文档列表中呢?更方便启动工作流!更多用途,大家可以探讨下,有更好的方法,请共享!谢谢!