我为 wiki 页面库创建了一个新的内容类型。我通过代码将此内容类型添加到库中(接口不允许这样做)。接下来,我向此内容类型添加了一个事件接收器(在 ItemAdded 和 ItemAdding 上)。我的问题是没有触发任何事件。如果我将此事件直接添加到 wiki 页面库中,则一切正常。有限制/错误/技巧吗?
我使用 SharePoint Manager 查看了附加到库的内容类型,在他的架构中缺少事件接收器的部分......我知道应该有类似的内容:
<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>
如果我使用 SPM 查看添加到站点的内容类型,我会在架构中看到这部分。
这是我的代码:
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();
}
谢谢 !