none
Write event handler for a content type

    Question

  • 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-

    1. Create content type.
    2. Create .cs file and overide the item updating event.
    3. 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.


    Monday, March 05, 2012 1:32 PM

Answers

  •  

    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,
    Qiao


    Qiao Wei

    TechNet Community Support

    Thursday, March 08, 2012 10:54 AM

All replies

  • check the below link:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/fcd9e3b8-88c7-4dee-b37b-73bdbc6a3513

    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:

    http://sharepointx-men.com/2010/07/09/sharepoint-2010-event-receivers-part-1-create-event-receiver-for-content-types/

    http://blogs.msdn.com/b/brianwilson/archive/2007/03/18/event-handlers-part-3-register-event-handlers-plus-free-site-settings-manage-event-handlers-add-on.aspx


    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



    Monday, March 05, 2012 2:30 PM
  • 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

    Tuesday, March 06, 2012 6:34 AM
  •  

    Hi Seems_Sharepoint,

    Please follow the steps below to check whether it works.

    1. Create an empty SharePoint project.
    2. Create your content type through (the project->add item->content type)
    3. 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,
    Qiao


    Qiao Wei

    TechNet Community Support

    Wednesday, March 07, 2012 4:36 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


    Wednesday, March 07, 2012 9:26 AM
  •  

    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,
    Qiao


    Qiao Wei

    TechNet Community Support

    Wednesday, March 07, 2012 10:37 AM
  • 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

    Wednesday, March 07, 2012 1:36 PM
  •  

    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,
    Qiao


    Qiao Wei

    TechNet Community Support

    Thursday, March 08, 2012 10:54 AM
  • Thanks Qiao.

    It worked as per your suggestion.

    :)

    Wednesday, April 04, 2012 12:02 PM