如何将事件处理程序附加到 SharePoint 中的特定列表?

发布于 2024-10-22 05:36:36 字数 321 浏览 2 评论 0 原文

我已经读过这个问题如何附加事件接收器到共享点中的自定义列表?但我有疑问。

当我使用 EventReceivers.Add 方法附加事件接收器时,是否必须激活事件接收器功能,或者安装它就足够了吗?

而且,如果必须安装,我应该在事件功能的 elements.xml 中使用什么 ListTemplateId?

提前致谢

Ive already read this question how to attach an event receiver to a custom list in sharepoint? but I have a doubt.

When I use the EventReceivers.Add method to attach my event receiver, does the event receiver feature must be activated, or is it enough to have it installed?

And, if it has to be installed, what ListTemplateId should I use in the elements.xml of the event feature?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-10-29 05:36:36

事件接收者是一个定义。该定义映射到程序集和类实例。使用列表模板时,以声明方式使用事件接收器关联功能(指定 listtemplateid)。当您根据不唯一的列表类型附加到特定列表时,通常会使用对象模型。例如,通过使用 TemplateTypeID='104',将与具有该模板类型的所有列表关联。

使用对象模型允许您识别特定实例,以便您可以简单地创建关联。因此,如果您想将事件接收器部署为功能,您将拥有一个加载程序集的功能和一个功能接收器代码,该代码将通过对象模型为您创建关联。

希望这是有道理的。

The event receiver is a definition. The definition maps to an assembly and class instance. Using a feature for event receiver association declaratively(specifying listtemplateid) is done when using List Templates. When you are attaching to an specific list based on a list type that is not unique you generally use the object model. By using TemplateTypeID='104', for example, would associate with all lists with that template type.

Using the object model allows you to identify a specific instance so you can simply creates the association. So if you wanted to deploy an event receiver as a feature, you would have a feature that loads the assembly and a feature receiver code that would create the association for you via the object model.

Hopefully this makes sense.

秋意浓 2024-10-29 05:36:36

您只需为事件接收器部署解决方案。这使得代码可以附加到列表中。

要将事件接收器附加到特定列表,请在控制台应用程序中使用以下代码:

using (SPSite site = new SPSite(url))
{
    using (SPWeb siteWeb = site.OpenWeb())
    {
         SPList list = siteWeb.Lists["TheList"];

         SPEventReceiverDefinition defItemAdding = list.EventReceivers.Add();

         defItemAdding.Assembly = "MyEventHandlerProject, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=placeTokenHere";
         defItemAdding.Class = "MyEventHandlerProject.ClassName";
         defItemAdding.Name = "ItemAdding Event";
         defItemAdding.Type = SPEventReceiverType.ItemAdding;
         defItemAdding.SequenceNumber = 1000;
         defItemAdding.Synchronization = SPEventReceiverSynchronization.Synchronous;

         defItemAdding.Update();

}
你就

完成了!

You only need to deploy the solution for your event receiver. This makes the code available to attach to a list.

To attach the event receiver to a specific list use the following code in a console application:

using (SPSite site = new SPSite(url))
{
    using (SPWeb siteWeb = site.OpenWeb())
    {
         SPList list = siteWeb.Lists["TheList"];

         SPEventReceiverDefinition defItemAdding = list.EventReceivers.Add();

         defItemAdding.Assembly = "MyEventHandlerProject, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=placeTokenHere";
         defItemAdding.Class = "MyEventHandlerProject.ClassName";
         defItemAdding.Name = "ItemAdding Event";
         defItemAdding.Type = SPEventReceiverType.ItemAdding;
         defItemAdding.SequenceNumber = 1000;
         defItemAdding.Synchronization = SPEventReceiverSynchronization.Synchronous;

         defItemAdding.Update();

}
}

And you're done!

无尽的现实 2024-10-29 05:36:36

您可以创建一个控制台应用程序,将事件处理程序附加到 Sharepoint 列表。检查以下链接以获取示例控制台应用程序的代码。

http://ceprogrammingnotebook.blogspot.sg/2013/10 /attaching-event-handler-to-sharepoint.html

You can create a console application that will attach an event handler to a Sharepoint list. Check the below link for the code of the sample console application.

http://ceprogrammingnotebook.blogspot.sg/2013/10/attaching-event-handler-to-sharepoint.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文