【问题标题】:ItemUpdating called twice after ItemAdded in event receiverItemUpdating 在事件接收器中的 ItemAdded 之后调用了两次
【发布时间】:2012-04-04 16:05:44
【问题描述】:

我创建了一个事件接收器来处理 SharePoint 2010 中文档库上的 ItemAdded 和 ItemUpdating 事件。

我遇到了一个问题,当我将文档添加到库中时(例如,通过将其从 Word 中保存回来),正确调用了 ItemAdded 方法,但是随后两次调用 ItemUpdating。我已经从我的处理程序中删除了所有代码,以确保不是我在里面做的事情导致了问题。它们实际上看起来像:

public override void ItemUpdating(SPItemEventProperties properties)
{
}

public override void ItemAdded(SPItemEventProperties properties)
{
}

有人有解决这个问题的办法吗?

这是我用于事件接收器的 elements.xml 文件:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
      <Receiver>
        <Name>DocumentsEventReceiverItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.Namespace.DocumentsEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
      <Receiver>
        <Name>DocumentsEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.Namespace.DocumentsEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

【问题讨论】:

    标签: sharepoint-2010


    【解决方案1】:

    问题是在Item Updating 期间的Document library 事件处理程序还检查Document 是处于签入 模式还是签出。这就是它被调用两次的原因。

    你应该把你的代码放在

     public override void ItemUpdating(SPItemEventProperties properties)
      {  
        base.ItemUpdating(properties);
        if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
         {
           //do stuff
         }
      }
    

    更多细节Here是描述Document's事件整体情况的好文章。

    【讨论】:

    • 我的 if 语句与您的不完全相同,但我可以使用您提到的字段。在我的实际更新期间,之前和之后属性中的 vti_sourcecontrolcheckedoutby 始终为空,所以我可以检查一下。在 ItemAdded 之后调用 ItemUpdating 时,两者都填充了我的用户名
    • 好的,谢谢! ItemUpdating 仍然触发,但 if 语句让我确定是否实际运行我的代码
    • 是的..就是我的观点!!据我所知,它总是开火两次。我们从不阻止它,但我们使用此语句多次停止运行代码。
    • 我和你有同样的问题,你能告诉我你使用的语句是什么。 properties.AfterProperties["vti_sourcecontrolcheckedoutby"] 和 properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] 始终为空,我无法知道 ItemUpdated 是单独触发还是在添加文件后触发。
    猜你喜欢
    • 2011-08-12
    • 1970-01-01
    • 2012-02-07
    • 2012-12-11
    • 2011-10-16
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多