How do you bound an event receiver to a *specific* custom content type?

隐身守侯 提交于 2019-12-05 02:06:34

问题


I've created an event receiver and have added to the GAC.

How do I bound an event receiver to a specific custom content type?

I need to do this from an XML file:

So far I have:

Feature.xml that points to an Elements.xml file but am not sure about the Elements.xml file.

How do you reference a specific content type? (I have the guid for the specific content-type)


回答1:


To bind an event receiver to a specific content type, you use the XmlDocuments element of the content type elements file. Here's a rather exclusive excerpt from my upcoming book "Building the SharePoint User Experience":

And, just to clarify, since I don't see the XML you tried to post, you would add this to the content type declaration in the elements.xml file.

(...)

Attaching event receivers to content types using features

The important thing with attaching event receivers like we just saw is to notice that there is no way to bind a receiver to a content type, only to a list template.

No, there are no ways to add receivers to individual lists either. However, as we saw in the Email enabling custom lists section in the Forest of lists chapter we can programmatically add event receivers using code. We can use the same technique to add event receivers to a content type as well, but there should be a way to do this using CAML in a feature. After all that is where we define the content type, as we will see later in the chapter.

The answer? Event receivers in content types are defined in a different section of the elements file. Again we look to XmlDocuments and the third built-in XmlDocument type Receiver. The Receiver XmlDocument resides in the http://schemas.microsoft.com/sharepoint/events namespace. If you want to see a Receiver XmlDocument in your default SharePoint installation, search the ctypeswss.xml file for the content type 0x010107, the DocumentWorkflowItem:

<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
 <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
  <Receiver>
   <Name>Workflow Library Item Added</Name>
   <Type>ItemAdded</Type>
   <SequenceNumber>1</SequenceNumber>
   <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,~CCC
    PublicKeyToken=71e9bce111e9429c</Assembly>
   <Class>Microsoft.SharePoint.Workflow.SPWorkflowLibraryEventReceiver</Class>
   <Data />
   <Filter />
  </Receiver>
… (snipped for space saving purposes
 </Receivers>
</XmlDocument>

Note The example uses a prefix spe: for the Receivers element. This is not necessary from a technical point of view, but can add clarity to your code. As you can see, the Receiver element follows the structure of a regular item event receiver as defined in elements. Except, of course, that the receiver is defined in the XmlDocument section of the content type.




回答2:


@Larsi: Same problem here. Looks like "Inherits" set to true is causing a bug and then XmlDocuments is completely ignored :@ Therefore, we are fixing this in a custom feature and binding the event receivers using object model.



来源:https://stackoverflow.com/questions/561835/how-do-you-bound-an-event-receiver-to-a-specific-custom-content-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!