我为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();
}
Thx!
答案 0 :(得分:5)
我不确定,但是在将内容类型添加到列表之前,您是否可以尝试将事件接收器添加到内容类型。
我认为之前必须添加事件接收器,因为在向列表添加内容类型时,内容类型不会直接添加到列表中,而是将其副本添加到列表中。因此,当您将内容类型添加到列表时,还没有事件接收器。