今天来给大家介绍关于EventHander的编程,在我们的实际MOSS的开发过程中,会遇到很多关于EventHander开发的需求,如,当我们在LIST里面创建一个ITEM的时候 ,我们希望做那些事情,监控那个LIST的时候,我们就可以用EventHander来做拉,在开发EventHander的时候,类库需要强命名,需要把开发的EventHandeR放到GAC里面,还要必须继承SPItemEventReceiver 类,这样就可以重在很多方法拉。那些方法 ,我相信大家看看就明白拉。下面是我测试的一个Demo!
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace LhEventHandlerConsole
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite(@"http://lh-vmpc/personal/test/ShowJob");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["SearchJob"];
string Name = "LHEventHandler,Version=1.0.0.0,Culture=Neutral,PublicKeyToken=3c5da5cf700fd014,processorArchitecture=MSIL";
string className = "LHEventHandler.ShowEventHandler";
list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Name, className);
list.EventReceivers.Add(SPEventReceiverType.ItemAdded,Name,className);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, Name, className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, Name, className);
Console.WriteLine("It's OK");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace LHEventHandler
{
class ShowEventHandler:SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
//base.ItemAdding(properties);
SPListItem item = properties.ListItem;
string name = item["SearchResult"].ToString();
string date = Convert.ToDateTime(item["SearchJobTime"]).ToShortDateString();
item["标题"] = name + date+"AA";
item.Update();
}
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemAdded(properties);
SPListItem item = properties.ListItem;
string name = item["SearchResult"].ToString();
string date = Convert.ToDateTime(item["SearchJobTime"]).ToShortDateString();
item["标题"] = name+date;
item.Update();
}
public override void ItemUpdated(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
string name = item["SearchResult"].ToString();
string date = Convert.ToDateTime(item["SearchJobTime"]).ToShortDateString();
item["标题"] = name + date;
item.Update();
}
public override void ItemDeleting(SPItemEventProperties properties)
{
//base.ItemDeleting(properties);
properties.ErrorMessage = "ShowGm say:~~~New no Delete Info!";
properties.Cancel = true;
}
}
}