編輯:我意識到我的方法在第二個代碼塊是不必要的。我可以做完成同樣的事情在ItemUpdated如下:如何通過事件接收器更新當前項目中的字段值?
SPListItem thisItem = properties.ListItem;
thisItem.File.CheckOut();
thisItem["Facility Number"] = "12345";
thisItem.Update();
thisItem.File.CheckIn("force check in");
不幸的是,我仍然得到同樣的錯誤消息時「thisItem.Update();」被執行:他沙盒代碼執行請求被拒絕,因爲沙盒代碼主機服務太忙,無法處理請求
我實際上在接收上面的錯誤時,最初部署我的沙盒解決方案並使用此鏈接(http:// blogs .msdn.com/b/sharepointdev /存檔/ 2011/02/08 /錯誤的,沙盒,代碼執行請求 - 在 - 拒絕 - 因爲最沙盒代碼主機服務,是太忙碌-to-handle-the-request.aspx)來修復它。
我想寫一個C#事件接收器改變,當文檔被添加/庫中的改變字段的值。我曾嘗試使用以下代碼:
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
string fieldInternalName = properties.List.Fields["Facility Number"].InternalName;
properties.AfterProperties[fieldInternalName] = "12345";
}
不幸的是,這隻適用於某些領域。例如,如果我將「Facility Number」替換爲「Source」,則代碼將正確執行。這可能是因爲我們使用的是第三方軟件(稱爲KnowledgeLake),它使用Silverlight表單替換SharePoint中的默認編輯表單。無論如何,因爲我是有上面的代碼的挑戰(再次,因爲我認爲Silverlight的形式可能會改寫ItemUpdating事件觸發後場),我曾嘗試下面的代碼:
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
//get the current item
SPListItem thisItem = properties.ListItem;
string fieldName = "Facility Number";
string fieldInternalName = properties.List.Fields[fieldName].InternalName;
string fieldValue = (string)thisItem["Facility Number"];
if (!String.IsNullOrEmpty(fieldValue))
{
//properties.AfterProperties[fieldInternalName] = "123456789";
SPWeb oWebsite = properties.Web as SPWeb;
SPListItemCollection oList = oWebsite.Lists[properties.ListTitle].Items;
SPListItem newItem = oList.GetItemById(thisItem.ID);
newItem.File.CheckOut();
thisItem[fieldInternalName] = "12345";
thisItem.Update();
newItem.File.CheckIn("force");
}
}
首先,在上面對我來說看起來有點小,因爲我喜歡只使用AfterProperties方法。另外,執行「newItem.Update()」時出現以下錯誤:由於沙盒代碼主機服務太忙而無法處理請求,因此他拒絕沙盒代碼執行請求
我在這裏丟失了什麼嗎?我很想利用第一個代碼塊。任何幫助,將不勝感激。
得到這個想通了。我不得不用base.EventFiringEnabled = false包裝我的代碼; ...運行代碼... base.EventFiringEnabled = true; – Josh 2012-08-08 16:06:49
那個伎倆,謝謝。 – 2012-10-02 15:56:43
@Josh - 所以請把它寫成答案,以幫助任何有這個問題的人。 – banana 2013-07-09 10:01:50