2012-02-23 26 views
1

我有一個自定義列表定義,其中包含一個覆蓋ItemUpdating的事件接收器。該列表有內容批准開啓,連同創建主要和次要版本在ItemUpdating中設置SPListItem字段不是持久的SystemUpdate(false)

如果項目被批准,我想設置一個布爾型字段(發佈?),而不會影響版本和審批狀態。據我所知系統更新(假)是假設這樣做,但它不會堅持布爾值。如果我使用更新()SystemUpdate(),該值仍然存在,但它不審批狀態設置爲批准並引發以下錯誤:

The file [filename] has been modified by [user] on [date].

public override void ItemUpdating(SPItemEventProperties properties) 
{ 
    base.ItemUpdating(properties); 
    EventFiringEnabled = false; 
    try 
    { 
     if (IsChangingToApproved(properties)) 
     {  
      if (!Validate(properties)) 
      {// This person can't approve 
       properties.ErrorMessage = "You don't have appropriate permissions."; 
       properties.Status = SPEventReceiverStatus.CancelWithError; 
       properties.Cancel = true; 
      } 
      else 
      {// Set the IsPublished flag to true       
       var isPublishedField = properties.List.Fields["Is Published?"]; 
       if (isPublishedField != null) 
       { 
        properties.ListItem[isPublishedField.InternalName] = true; 

        // Doesn't update bool, ItemUpdating event functions normally 
        properties.ListItem.SystemUpdate(false); 

        // Updates bool, but ItemUpdating event does not complete 
        //properties.ListItem.Update(); 
        //properties.ListItem.SystemUpdate(); 

       } 
      } 
     } 
    } 
    catch (Exception ex) { return; } 
    finally { EventFiringEnabled = true; } 
} 

事情我已經嘗試過:

  • 使用using Site/using Web塊更新listItem,而不是從屬性更新項目。
  • 設置properties.AfterProperties [「Is Published?」]字段。

回答

2

您不應該在同步事件中調用系統更新。沒有添加額外版本的事件。

如果要在更新之前更新屬性,可以更改afterProperties [「」],如果更新成功,則更改將保留。

base.ItemUpdating(properties); 
properties.AfterProperties["Is Published"] = true; 

通過你也可以檢索使用 ListItem.ModerationInformation.Status == SPModerationStatusType.Approved(=發佈和批准)

依託開箱即用的內場將確保你不會有額外的事件惹公佈情況的方式接收器(注意有趣的東西,比如內容部署正在運行......),並確保狀態始終保持最新狀態。

希望它有幫助。

+0

在同步事件中更新您是正確的。我使用異步ItemUpdated事件,因爲設置AfterProperties不適合我。我正在標記你的答案是正確的。 – rboone 2012-02-23 19:41:20

+0

很高興我能夠幫助:)接收器並不容易掌握,即使經過幾年與SharePoint的合作,我仍然每天都在學習。祝你的項目好運。 – 2012-02-23 20:14:41