为SharePoint添加Event Receiver
为列表类型绑定Event Receiver可以通过添加一个feature, 并在feature中添加一个receiver element来完成.
具体详见SharePoint中的EventReceiver 之二 绑定Event Receiver到列表类型或列表实例.
除了可以为列表类型添加event receiver以外, MOSS 2007还可以在如下的三种对象的实例上添加Event Receiver.
- SPList
- SPContentType
- SPWeb
为他们添加event receiver可以通过SharePoint Object Model来完成, 核心代码为以下的函数调用.
SPEventReceiverDefinitionCollection.Add
有一个免费的为sharepoint中的列表, contenttype, 站点添加event receiver的工具, 非常好用. 点击这里下载可执行文件和源代码.
添加的关键代码片段如下:
if (node.Tag is SPList) { SPList list = (SPList)node.Tag; list.EventReceivers.Add ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text), textBoxAssemlby.Text, comboBoxClasses.Text); list.Update(); flag = true; } if (node.Tag is SPContentType) { SPContentType ct = (SPContentType)node.Tag; ct.EventReceivers.Add ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text), textBoxAssemlby.Text, comboBoxClasses.Text); ct.Update(); flag = true; } if (node.Tag is SPWeb) { SPWeb w = (SPWeb)node.Tag; w.EventReceivers.Add ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text), textBoxAssemlby.Text, comboBoxClasses.Text); w.Update(); flag = true; }
移出的关键代码如下:
TreeNode node = treeViewItems.SelectedNode; SPEventReceiverDefinition er = (SPEventReceiverDefinition)node.Tag; if (MessageBox.Show("Are you sure you want to remove this event handler?", "Event Handler Explorer",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { er.Delete();
下面的连接有介绍使用方法.
Free Tool: Register Event Handlers Programmatically (WSS 2007)
http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx?ID=1547