在ListAdded List事件接收器

时间:2016-04-26 13:25:46

标签: c# sharepoint programmatically-created event-receiver

每当使用Sandbox解决方案创建特定列表时,我都会尝试以编程方式在SharePoint 2010中创建日历列表​​。我已经实现了一个ListAdded ListEventReceiver,以便运行代码来生成日历。

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>

2 个答案:

答案 0 :(得分: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
...

答案 1 :(得分: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秒。如果在单个请求期间超出此时间限制,则用户代码服务将回收相关应用程序域,并且请求将返回错误。