2016-04-26 82 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)。任何其他想法可能会导致这种情况? –