locked
How to attach event receiver to exists Content Type and not to every list on the site? RRS feed

  • Question

  • Hi All,

    I have few Content Types A,B, C that were inherided from BaseContentType. I need to fire my Event Receiver just on lists where Content Types A,B and C were added. I cannot touch content types on the server. So, I created a feature receiver to attach the event receiver to BaseContentType content type.

    My issue is the event receiver faired for every list on the site. How to limit it just by BaseContentType?

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers>
          <Receiver>
            <Name>DocumentsManagementItemCheckingOut</Name>
            <Type>ItemCheckingOut</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
            <Class>FEEventReceivers.DocumentsManagement</Class>
            <SequenceNumber>10000</SequenceNumber>
          </Receiver>
          <Receiver>
            <Name>DocumentsManagementItemAttachmentDeleting</Name>
            <Type>ItemDeleting</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
            <Class>FEEventReceivers.DocumentsManagement</Class>
            <SequenceNumber>10000</SequenceNumber>
          </Receiver>
      </Receivers>
    </Elements>
    SPWeb site = properties.Feature.Parent as SPWeb;
    SPWeb web = site.Site.RootWeb;
    DeleteEventDefinitions(web);
    SPContentType contentType = web.ContentTypes[STR_CONTENT_TYPE_NAME];
    SPEventReceiverDefinition eventReceiverDefinitionWorkflowStarting = contentType.EventReceivers.Add();
    SPEventReceiverDefinition eventReceiverDefinitionItemDeleting = contentType.EventReceivers.Add();
    eventReceiverDefinitionItemDeleting.Class = STR_DM_CLASS_NAME;
    eventReceiverDefinitionItemDeleting.Assembly = STR_ASSEMBLY_NAME;
    eventReceiverDefinitionItemDeleting.Type = SPEventReceiverType.ItemDeleting;
    eventReceiverDefinitionItemDeleting.Update();
    SPEventReceiverDefinition eventReceiverDefinitionItemCheckingOut = contentType.EventReceivers.Add();
    eventReceiverDefinitionItemCheckingOut.Class = STR_DM_CLASS_NAME;
    eventReceiverDefinitionItemCheckingOut.Assembly = STR_ASSEMBLY_NAME;
    eventReceiverDefinitionItemCheckingOut.Type = SPEventReceiverType.ItemCheckingOut;
    eventReceiverDefinitionItemCheckingOut.Update();
    contentType.Update(true);
    



    Oleg

    Wednesday, January 16, 2013 9:33 PM

Answers

All replies

  • Hi Oleg,

    The first part of the code i.e. feature xml is not required, if you use this the event receiver gets attached to the list and not to the content type, So in your case the event receiver are getting attached to both  list and content type.

    You need to create a class inheriting from SPItemEvent receiver and then add that event receiver only to the content type and not to the list.

    Go through the links below it will  help you to understand and implement event receiver on content type

    http://ybbest.wordpress.com/2012/03/17/how-to-add-event-receiver-to-sharepoint2010-content-type-programmatically/

    http://karinebosch.wordpress.com/walkthroughs/event-receivers-walkthrough3/

    http://malcan.com/EN/Lists/Tips%20and%20tricks/DispForm.aspx?ID=26

    http://charliedigital.com/2010/10/06/programmatically-adding-an-event-receiver-to-a-content-type/

    Hope this helps


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Thursday, January 17, 2013 5:41 AM
  • I have removed event receivers elements from my feature and my events do not fire anymore. I am attaching event receivers to BaseContentType. Is child content types inherited Event Receiver from BaseContentType?

    Oleg

    Thursday, January 17, 2013 7:53 PM
  • Hi Oleg,

    Child content types will inherit event receiver from parent content type.

    Have a look at the following article,

    http://pholpar.wordpress.com/2010/08/06/event-receiver-inheritance-between-parent-and-child-content-types/

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Friday, January 18, 2013 5:29 AM
  • you have to attach the event to child content types only i.e. content type A, B and C in your scenario. If you attach to base content type .i.e. parent content type, then the event receiver will fire for all the lists which as the base content type

    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, January 18, 2013 7:10 AM