Robert Brown2


Hi

Im having troubles creating an event handler for a specific content type in share point. I managed to get the event handler to work for all document types when they are uploaded by using a ListTemplateID of 101. How do i get the ListTemplateID for a custom content type

This site said it was possible to add event handlers for a specific content type http://msdn2.microsoft.com/en-us/library/ms475328.aspx.

any help would be greatly appreciated.

Rob



Re: Creating an event handler for a custom content type

Ishai Sagi


Rob,

good catch.

Searching the web, I saw the following example: http://www.sharepointblogs.com/tonstegeman/archive/2007/02/15/19518.aspx

I think this may have to be done by code and not by features. I hope this is wrong.

This gave me an idea to add to my utility pack, to register an event handler to a content type and not just to a library. But until I do that, you are on your own.






Re: Creating an event handler for a custom content type

Robert Brown2

Thanks Ishai,

I managed to find a tool that alows you to add event handlers to just about anything in sharepoint here is the link http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx ID=1547.

However this tool does not create a feature for your event handlers so you cannot activate and deactivate them unless you remove them with the tool again (not the optimal solution).

I also found a way to install my event handlers as features on predefined content types (using the XML files) but it doesnt seem to be firing the event when i do it either way (If i use the tool or i do it this way).

I created a document library specificaly for this content type, and the idea is when ever this content type is uploaded the event is fired and the data in the file is then processed.

here is how i did the install.

Feature.xml

<Feature
Scope="Site"
Title="Simple Event Handler"
Id="dbb8da7a-6964-4487-91c9-4e29fcf64542"
xmlns="http://schemas.microsoft.com/sharepoint/"
Description="This is to test the useage of event handlers">
<ElementManifests>
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
</Feature>

Elements.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ContentType ID="0x01010075F2AFC3AB2A444EB3B2608A4F61E96F" Name="Content Type 1" Group="Custom Content Types" Description="This is to test the event management system" Version="0">
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
<spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
<Receiver>
<Name>ItemAdding</Name>
<Type>ItemAdding</Type>
<SequenceNumber>1</SequenceNumber>
<Assembly>SimpleEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0e25abe2b7d6001b</Assembly>
<Class>SimpleEventHandler.SimpleEventHandler</Class>
<Data>
</Data>
<Filter>
</Filter>
</Receiver>
<Receiver>
<Name>ItemUpdating</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>2</SequenceNumber>
<Assembly>SimpleEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0e25abe2b7d6001b</Assembly>
<Class>SimpleEventHandler.SimpleEventHandler</Class>
<Data>
</Data>
<Filter>
</Filter>
</Receiver>
</spe:Receivers>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>

The hilighted section was important, the Content type id can be found when you are in sharepoint and are viewing the content type, look in the address bar for this. Any variations in this will create a new Content Type.

Do you know any way that i can test to see why the event does not fire when it on a content type

Rob






Re: Creating an event handler for a custom content type

Ishai Sagi

Rob,

I have a sinking feeling that I know your problem.

If you had read my blog, you would have come across a post from a couple of months back about event handlers and the ItemAdding event. I think your code is trying to read and\or write to the item properties in the ItemAdding event, which is impossible since when you do "properties.ListItem" you get null.

if you debug your event handler you will find that it is catching the event, but failing on error the first time you try to access the list item in the ItemAdding event.

Check it and let me know if that was indeed the problem.






Re: Creating an event handler for a custom content type

Robert Brown2

Hi Ishai

I tried debugging and the event handler is never reached when it is registerd on a content type. The same .dll registered on all document libraries works just fine. The tool that i mentioned in the previous post show that there are event handlers for the content type i created but they dont seem to be working. (http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx ID=1547)

I checked in the features list and the feature is active, Im not sure why my event code is never reached when it is registered agains a specific content type.

Rob





Re: Creating an event handler for a custom content type

Morten Andreasen

Hey Robert

I have the exact same problem, did you manage to get the events fired on the content type





Re: Creating an event handler for a custom content type

Robert Brown2

Hi Morten,

I didnt get the event to fire when it was registered on a content type, we decided to go with document libraries for specific content types.

Try using that tool i mentioned earlier, it is pretty cool.





Re: Creating an event handler for a custom content type

Jian Zhang

I tried to make things work by deploying it as a feature. As long as you know which listtemplateid you are binding the event to... It could be the wrong id your are targetting if it doesn't get fired while you successfully deployed a feature and activated it.




Re: Creating an event handler for a custom content type

Robert Brown2

We can get the events to work on specific lists by delopying them as features (lists such as document libraries) but when you deploy your event manager as a feature to capture events when a specific content type is uploaded it does not work. the feature is active but the event never gets fired. using the tool you can see there is an event reciever but this still doesnt work.





Re: Creating an event handler for a custom content type

Morten Andreasen

Hi Robert

I have successfully gotten a eventreciever registred to a custom contenttype to fire events - but only if i register the eventreciever using a feature.

If I use the tool from u2u the eventreciever does not fire any events.....strange! mabey I should try to call mycustomcontenttype.Update(true) or something similar...anyone





Re: Creating an event handler for a custom content type

Morten Andreasen

Yes, that did the trick

When registrering eventrecievers for contenttypes, mak sure to call YourContentType.Update(true) after you have added the EventRecieverDefinition, for the events to fire.

When removing eventrecivers for contenttype, make sure to call YourContentType.Update(true) after you have called the Delete() method on the EventRecieverDefinition.

I hope that helps





Re: Creating an event handler for a custom content type

Robert Brown2

Thanks Morten.



Re: Creating an event handler for a custom content type

Thomas

Morten, thanks for posting your solution! I'm afraid I'm not quite getting it - from where do I need to call MyContentType.Update(true) Are you registering you event handlers from code, or what am I missing Shouldn't the Feature XML (declarative approach) be enough



Re: Creating an event handler for a custom content type

Amplus

Hi,

That would be the SPContentType's method Update after you have called SPContentType.EventReceivers.Add

/Peter Karpinski





Re: Creating an event handler for a custom content type

alexneb

Hi All,
This thread was quite helpfull for my small task with WSS 3.0 event handler. Thanks to all!
I have encountered a different problem though : after I've created WSS 3.0 event handler and registered it (as a feature) with custom content type. The content type has a boolean column the value of which is used in my event handler code.
I am able to find the content type and its column at run time (properties.ListItem.ContentType.Fields.GetField(siteColumnType)). But I don't seem to be able to squeeze the value out of this SPField (it is SPFieldBoolean actually).
All available methods GetFieldValue() , HetFieldValueAsText() and ...AsHtml() takes either object or string as argument. And none of them seem to work (at least for me). I wonder if there a straightforward (or any!) way to get the value out of SPField without creating a custom filed and overriding its GetValue methods
Thanks a lot in advance ,
Alex