维基页面库上未触发内容类型的事件接收器

发布于 2024-08-25 02:53:40 字数 4044 浏览 4 评论 0原文

我为 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();
    }

谢谢!

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-09-01 02:53:40

我不确定,但是您可以在将内容类型添加到列表之前尝试将事件接收器添加到内容类型吗?

我认为必须之前添加事件接收器,因为将内容类型添加到列表时,内容类型不会直接添加到列表中,而是将其副本添加到列表中。因此,当您将内容类型添加到列表时,还没有事件接收器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文