sharepointtypeseventreceiver

Event receiver on Content Type not triggered on WikiPageLibrary


I created a new content type for a wiki page library. I added this content type to library by code (the interface did not allow this). Next, I added an event receiver to this content type (on ItemAdded and ItemAdding). My problem is that no event is trrigered. If I add this events directly to the wiki page library all works fine. Is there a limitation/bug/trick ?

I looked at the content type attached to the library with SharePoint Manager and in his schema the part for event receiver is missing...I know that there should be something like:

<XmlDocuments>
    <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
      <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
        <Receiver>
          <Name>
          </Name>
          <Type>1</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>RssFeedWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6722cbeba696def</Assembly>
          <Class>RssFeedWP.ItemEventReceiver</Class>
          <Data>
          </Data>
          <Filter>
          </Filter>
        </Receiver>
        <Receiver>
          <Name>
          </Name>
          <Type>10001</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>RssFeedWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6722cbeba696def</Assembly>
          <Class>RssFeedWP.ItemEventReceiver</Class>
          <Data>
          </Data>
          <Filter>
          </Filter>
        </Receiver>
      </spe:Receivers>
    </XmlDocument>

If I look with SPM to the content type added to site I see this part into schema.

Here is my code:

   public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            // create RssWiki content type
            SPContentType rssFeedContentType = new SPContentType(web.AvailableContentTypes["Wiki Page"], 
                                                web.ContentTypes, "RssFeed Wiki Page");

            // add rssfeed url field to the new content type
            AddFieldToContentType(web, rssFeedContentType, "RssFeed Url", SPFieldType.Note);                
            // add use xslt check box field to the new content type
            AddFieldToContentType(web, rssFeedContentType, "Use Xslt", SPFieldType.Boolean);                
            // add xslt url field to the new content type
            AddFieldToContentType(web, rssFeedContentType, "Xslt Url", SPFieldType.Note);

            web.ContentTypes.Add(rssFeedContentType);
            rssFeedContentType.Update();
            web.Update();

            AddContentTypeToList(web, rssFeedContentType);
            AddEventReceiversToCT(rssFeedContentType);
            //AddEventReceiverToList(web);
        }
    }

   private void AddFieldToContentType(SPWeb web, SPContentType ct, string fieldName, SPFieldType fieldType)
    {            
        SPField rssUrlField = null;
        try
        {
            rssUrlField = web.Fields.GetField(fieldName);
        }
        catch (Exception ex)
        {
            if (rssUrlField == null)
            {
                web.Fields.Add(fieldName, fieldType, false);
            }
        }
        SPFieldLink rssUrlFieldLink = new SPFieldLink(web.Fields[fieldName]);
        ct.FieldLinks.Add(rssUrlFieldLink);
    }

   private static void AddContentTypeToList(SPWeb web, SPContentType ct)
    {
        SPList wikiList = web.Lists[listName];
        wikiList.ContentTypesEnabled = true;
        wikiList.ContentTypes.Add(ct);
        wikiList.Update();
    }

   private static void AddEventReceiversToCT(SPContentType ct)
    {
        //add event receivers                
        string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
        string ctReceiverName = "RssFeedWP.ItemEventReceiver";

        ct.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, ctReceiverName);
        ct.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, ctReceiverName);
        ct.Update();
    }

Thx !


Solution

  • I'm not sure, but could you try to add the event receiver to the content type before you add the content type to the list.

    I think the event receiver has to be added before because when adding a content type to a list the content type is not added directly to the list, rather a copy of it is added to the list. So when you add your content type to the list, there is no event receiver yet.