2016-04-26 83 views
0

我試圖以編程方式在SharePoint 2010中創建日曆列表​​,只要使用沙盒解決方案創建特定列表。我已經實現了一個ListAdded ListEventReceiver以運行代碼來生成日曆。在ListAdded列表中創建列表事件接收器

public class GenerateCalendar : SPListEventReceiver 
{ 
    public override void ListAdded(SPListEventProperties properties) 
    { 
     base.ListAdded(properties); 

     // Exit out if this is not a MyList type 
     if(!IsMyList(properties)) 
     return; 

     string calendarTitle = properties.List.Title + " Calendar"; 

     SPWeb spWeb = properties.Web; 
     SPListTemplateType type = new SPListTemplateType(); 
     type = SPListTemplateType.Events; 

     // Execution breaks here: 
     Guid listGuid = spWeb.Lists.Add(calendarTitle, "Associated Calendar", type); 
     SPList newList = spWeb.Lists[listGuid]; 
     newList.OnQuickLaunch = properties.List.OnQuickLaunch; 
     newList.Update(); 
    } 
} 

當我打電話spWeb.Lists.Add(...),我得到一個SPException(沙盒代碼執行請求被拒絕,因爲沙盒代碼主機服務是太忙,無法處理請求。)

從MSDN文檔中,我可以看到在沙盒解決方案(https://msdn.microsoft.com/en-us/library/office/ms413986(v=office.14).aspx)中可用的SPListCollection.Add方法。在這種事件接收器中創建列表是否有限制?有誰知道爲什麼這不起作用?

編輯添加的生成的Feature.xml和Elements.xml的文件

的Feature.xml:

<?xml version="1.0" encoding="utf-8"?> 
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" 
    Title="Calendar Generator" 
    Description="Generates a calendar" 
    Id="dfe3388c-c063-4873-a41b-5c066907c510" 
    Scope="Web"> 
    <ElementManifests> 
     <ElementManifest Location="GenerateCalendar\Elements.xml" /> 
    </ElementManifests> 
</Feature> 

Elements.xml的

<?xml version="1.0" encoding="utf-8"?> 
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> 
    <Receivers > 
     <Receiver> 
     <Name>GenerateCalendarListAdding</Name> 
     <Type>ListAdding</Type> 
     <Assembly>MyListGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5cff2198a602ec41</Assembly> 
     <Class>MyListGenerator.Event_Receivers.GenerateCalendar.GenerateCalendar</Class> 
     <SequenceNumber>10000</SequenceNumber> 
     </Receiver> 
     <Receiver> 
     <Name>GenerateCalendarListDeleting</Name> 
     <Assembly>MyListGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5cff2198a602ec41</Assembly> 
     <Class>MyListGenerator.Event_Receivers.GenerateCalendar.GenerateCalendar</Class> 
     <SequenceNumber>10000</SequenceNumber> 
     </Receiver> 
     <Receiver> 
     <Name>GenerateCalendarListAdded</Name> 
     <Type>ListAdded</Type> 
     <Assembly>MyListGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5cff2198a602ec41</Assembly> 
     <Class>MyListGenerator.Event_Receivers.GenerateCalendar.GenerateCalendar</Class> 
     <SequenceNumber>10000</SequenceNumber> 
     </Receiver> 
    </Receivers> 
</Elements> 

回答

1

我找到了答案。顯然在這個事件接收器中創建一個列表導致了對事件接收器的遞歸調用,即使我已經在非MyList退出了基於模板的列表。解決方案是簡單地添加EventFiringEnabled = false

... 
SPWeb spWeb = properties.Web; 
SPListTemplateType type = new SPListTemplateType(); 
type = SPListTemplateType.Events; 

EventFiringEnabled = false; // Disable event firing and create the list 
Guid listGuid = spWeb.Lists.Add(calendarTitle, "Associated Calendar", type); 
SPList newList = spWeb.Lists[listGuid]; 
newList.OnQuickLaunch = properties.List.OnQuickLaunch; 
newList.Update(); 
EventFiringEnabled = true; // Re-enable event firing 
... 
0

您可以創建事件接收器,從派生沙盒解決方案中的SPListEventReceiver。但是,事件接收器必須在您的功能元素文件中註冊declaratively;它不能使用對象模型進行註冊(例如通過功能接收器)。

最有可能你正在運行到上資源點您的沙盒解決方案允許消費量的限制,雖然你也可以運行到能夠按要求被消耗的資源的絕對限制。

此外,如果您的環境運行速度特別慢,如果在單個請求期間超出了其時間限制(默認爲30秒),則用戶代碼服務將進行回收。

欲瞭解更多信息,請參閱Microsoft提供的本沙盒解決方案文檔的「瞭解解決方案監控」部分:https://msdn.microsoft.com/en-us/library/ff798382.aspx

相關摘錄如下。

資源點:

如果在網站集中的解決方案,超過該網站集的日常資源點分配時,SharePoint將在該網站集下線每一個沙盒解決方案爲一天的休息。

資源點是根據14種不同的測量值計算出來的,這些度量值稱爲資源度量值,包括CPU執行時間,內存消耗和未處理的異常。

...

的SharePoint都會計入總的解決方案中最昂貴的資源的措施,而不是所有措施的總和。

絕對限制:

爲了防止流氓沙盒解決方案從導致不穩定的SharePoint還監視每單個請求沙盒解決方案。 14個資源度量中的每一個都包含一個AbsoluteLimit屬性,該屬性定義沙盒解決方案在單個請求中可以使用的資源的硬限制。如果超過絕對限制,則SharePoint通過停止並重新啓動Sandbox工作進程來終止請求。例如,CPU執行時間資源度量具有60秒的默認絕對限制。如果單個請求執行時間超過60秒,則用戶代碼服務將停止並重新啓動執行請求的沙箱工作進程。

WorkerProcessExecutionTimeout:

另外,用戶碼服務包括名爲WorkerProcessExecutionTimeout具有30秒的缺省值屬性。如果在單個請求期間超出此時間限制,則用戶代碼服務將回收相關應用程序域,並且請求將返回錯誤。

+0

它似乎沒有任何這些東西。事件接收器是聲明式註冊的。我讓Visual Studio處理它,但我驗證了在鏈接的文檔中針對該示例生成的Feature.xml和Elements.xml,並且它匹配得相當好(除了元素的Receivers標記中缺少ListTemplateId屬性.XML)。我編輯了我的問題,並將文件的輸出添加到最後。 –

+0

至於** WorkerProcessExecutionTimeout **,我測試了幾次,看看我收到異常之前需要多長時間,並且一直持續10秒左右。我通過PowerShell驗證了** WorkerProcessExecutionTimeout **(設置爲30)的值,甚至嘗試將其更改爲60,但這沒有什麼區別。僅10秒後發生異常。資源使用量約爲0.07(儘管這個數字似乎從0到0.15)。任何其他想法可能會導致這種情況? –