Event receiver on Content Type not triggered on WikiPageLibrary
Posted
by Ciprian Grosu
on Stack Overflow
See other posts from Stack Overflow
or by Ciprian Grosu
Published on 2010-03-17T13:59:46Z
Indexed on
2010/03/17
14:01 UTC
Read the original article
Hit count: 480
Hello all,
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 !
© Stack Overflow or respective owner