Write event handler for a content type
-
Monday, March 05, 2012 1:32 PM
I have a requirement where any document library that inherits from a specific Content type an event should fire and redirect user to a custom page.
Steps I followed in VS 2010-
- Create content type.
- Create .cs file and overide the item updating event.
- Then in the element.xml file of conetent type added the following tags-
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
<Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
<Receiver>
<Name>ProtectiveMarkingEventHandler</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>2</SequenceNumber>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>DocLibContentType.ContentType1.ProtectiveMarkingEventHandler</Class>
</Receiver>
</Receivers>
</XmlDocument>
</XmlDocuments>But event is not firing.
Any guidance on the same will be helpful.
- Edited by Seems_Sharepoint Monday, March 05, 2012 1:32 PM
All Replies
-
Monday, March 05, 2012 2:30 PM
check the below link:
or try other option:
while attaching the receiver to document library you need use SPListTemplateType .
check the list of ids here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx
check the links below how to attach event receiver to a content type:
MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com
If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful
- Proposed As Answer by Arun Kumar Arora Monday, March 05, 2012 2:44 PM
- Edited by Devendra Velegandla Wednesday, February 06, 2013 8:38 AM
-
Tuesday, March 06, 2012 6:34 AM
Hi Devendra,
Thanks for your help.
I have followed the same steps as you specified.
But the event doesn't fire, nor am I able to set the debugger.
Please guide me on how to proceed.
Thanks
-
Wednesday, March 07, 2012 4:36 AMModerator
Hi Seems_Sharepoint,
Please follow the steps below to check whether it works.
- Create an empty SharePoint project.
- Create your content type through (the project->add item->content type)
- Create the event receiver through (the project->add item->event receiver, you can choose the event receiver to any of the lists)
Open the element.xml file of the content type, add the element as below:
<XmlDocuments><XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
<spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
<spe:Receiver>
<spe:Name>testCTERItemUpdating</spe:Name>
<spe:Type>ItemUpdating</spe:Type>
<spe:Assembly>$SharePoint.Project.AssemblyFullName$</spe:Assembly>
<spe:Class>testCT2.testCTER.testCTER</spe:Class>
</spe:Receiver>
</spe:Receivers>
</XmlDocument>
</XmlDocuments>
Notice: replace the Name, Type, Class element in the code snippet from the event receiver’s Elements.xml file.
If you have any more questions, please feel free to ask.
Thanks,
QiaoQiao Wei
TechNet Community Support
-
Wednesday, March 07, 2012 9:26 AM
Hi Qiao Wei,
Thank you for your response.
I have one query here. Do we need to reatin the element.xml file of the event reciever once we modify the element.xml file of content type.
Coz, when I remove the element.xml of event reciever, the event doesen't fire
- Edited by Seems_Sharepoint Wednesday, March 07, 2012 9:55 AM
-
Wednesday, March 07, 2012 10:37 AMModerator
Hi Seems_SharePoint,
I’m not clear about that why you remove the element file of the event receiver, the element like <spe:Name> needs to be same as in event receiver element file to find the relative event receiver.
If I have any misunderstanding, please feel free to let me know.
Thanks,
QiaoQiao Wei
TechNet Community Support
-
Wednesday, March 07, 2012 1:36 PM
Hi Qiao Wei,
I thought of removing the elements.xml file of the event reciever, coz I thought then the event will fire only for those document libraries, which inherit the custom content type I created and not the others.
Please correct me if my understanding is incorrect.
Thanks
-
Thursday, March 08, 2012 10:54 AMModerator
Hi Seems_SharePoint,
Sorry for mistake, add event receiver seems not work as expected.
In this situation, you may try to add the event receiver to the content type using object model, create the content type first, and also, you can add a class for the event receiver to the project, then try to use object model to add the event receiver to the content type, the code snippet is as below:
string className = "myCTTest.myCTER";string assemblyName = "myCTTest,Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6a1011fa4762354";
//string ertype = "";
SPContentTypeCollection ctcoll = web.ContentTypes;
foreach (SPContentType ct in ctcoll)
{
if (ct.Name.Equals("myCTTestCT"))
{
Console.WriteLine(ct.Name);
SPEventReceiverDefinitionCollection ercoll = ct.EventReceivers;
foreach (SPEventReceiverDefinition erd in ercoll)
{
if (erd.Class != className && erd.Assembly != assemblyName && erd.Type != SPEventReceiverType.ItemUpdating)
{
continue;
}
erd.Delete();
ct.Update(true);
break;
}
SPEventReceiverDefinition eventReceiverDefinition = ct.EventReceivers.Add();
eventReceiverDefinition.Class = className; // String
eventReceiverDefinition.Assembly = assemblyName; // String
eventReceiverDefinition.Type = SPEventReceiverType.ItemUpdating; // SPEventReceiverType
//eventReceiverDefinition.Data = documentType; // Arbitrary input data (String)
eventReceiverDefinition.Update();
ct.Update(true);
}
}
Thanks,
QiaoQiao Wei
TechNet Community Support
- Marked As Answer by Qiao WeiMicrosoft Contingent Staff, Moderator Friday, March 16, 2012 1:35 PM
-
Wednesday, April 04, 2012 12:02 PM
Thanks Qiao.
It worked as per your suggestion.
:)

