1.利用 Feature 来实现 EventHandler 与 SPList 的绑定:
public class FeatureReceiver : SPFeatureReceiver {
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPSite site = new SPSite(((SPSite)properties.Feature.Parent).Url))
{
SPList list = site.RootWeb.Lists[TestListName.TestA];
// 将 list.EventReceiver.Class==ListEventHandler的事件删除
DeleteEventHandler(list, typeof(ListEventHandler));
// 重新添加:事件类型、事件程序集名、事件程序集下的事件类名
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, typeof(FeatureReceiver).Assembly.FullName, typeof(TestEventHandler2.ListEventHandler).FullName);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, typeof(FeatureReceiver).Assembly.FullName, typeof(TestEventHandler2.ListEventHandler).FullName);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, typeof(FeatureReceiver).Assembly.FullName, typeof(TestEventHandler2.ListEventHandler).FullName);
}
}
catch (Exception ex)
{
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
/* no op */
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
/* no op */
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
/* no op */
}
/// <summary>
/// 删除指定SPList的事件句柄
/// </summary>
/// <param name="list">列表</param>
/// <param name="type">事件类的类型</param>
private void DeleteEventHandler(SPList list, Type type)
{
try
{
for (int i = list.EventReceivers.Count; i > 0; i--)
{
SPEventReceiverDefinition receiver = list.EventReceivers[i - 1];
if (receiver.Class.ToLower().Equals(type.FullName.ToLower()))
{
receiver.Delete();
}
}
}
catch (Exception ex)
{
}
}
}
2.事件操作:
public class ListEventHandler:SPItemEventReceiver
{
private static readonly string testA = TestListName.TestA;
private static readonly string testB = TestListName.TestB;
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
}
public override void ItemDeleting(SPItemEventProperties properties)
{
// Prevents events from being raised:阻止其他事件被调用
// 对应的是:EnableEventFiring()
this.DisableEventFiring();
int id = properties.ListItemId;
try
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb())
{
SPListCollection lists = web.Lists;
bool flag = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
for (int i = 0; i < lists.Count; i++)
{
if (lists[i].Title.Equals(testB))
{
SPList tempList = lists[i];
for (int j = 0; j < tempList.ItemCount; j++)
{
if (tempList.Items[j].ID.Equals(id))
{
tempList.Items[j].Delete();
tempList.Update();
}
}
}
}
web.AllowUnsafeUpdates = flag;
}
}
}
catch (Exception ex)
{
}
finally
{
}
}
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
}
}
常用的EventHandler事件有:
ItemAdding:添加前;
ItemAdded:添加后;
ItemUpdating:更新前;
ItemUpdated:更新后;
ItemDeleting:删除前;
ItemDeleted:删除后;
最常用的是:ItemAdded、ItemUpdating、ItemDeleting;
this.DisableEventFiring();是为了避免调用ItemEventReceiver事件。
可以参照以下资料:http://www.nanmu.net/keli/blog/Lists/Posts/Post.aspx?ID=38
(1).在开发SharePoint的EventHandler也许你会有这样的需求:在ItemUpdated事件中修改当前记录的某个字段的值,这样在修改的时候又会触发ItemUpdated事件,为了避免在此触发ItemUpdated事件,可以用this.DisableEventFiring()方法;
try
{
this.DisableEventFiring();
...
...
Item.Update();
}
finally
{
this.EnableEventFiring();
}
注:
也可以使用 SPItem.SystemUpdate()方法来更改,这样也不会触发ItemUpdated事件;
(2).在 SPItemEventReceiver.ItemUpdating, ItemAdding events:
properties.AfterProperties["Title"] = "the Title changed at " + DateTime.Now.ToString();
这样就可以更改SPItem.Title的内容,且不会触发任何的 ItemEventReceiver事件。