2012-02-17 36 views
1

我有一个“项目列表”(标题,主角,成员,站点URL),应该指的是具有项目列表的网站下的团队网站。所以我在沙盒解决方案中添加了一个SPItemEventReceiver到我的功能来做到这一点。Sharepoint:从一个项目事件处理程序创建一个子网站

ItemAdding(properties),我调用以下:

string projectName = properties.AfterProperties["Title"].ToString(); 
SPWeb currentWeb = properties.Web; 
SPWeb subweb = currentWeb.Webs.Add(projectName, projectName, 
    "Project site for " + projectName, (uint) currentWeb.Locale.LCID, 
    Microsoft.SharePoint.SPWebTemplate.WebTemplateSTS, true, false); 

但是,当调试,呼叫添加抛出SPException包装为与消息失败的HResult代码收到COMException沙盒代码执行请求被拒绝因为沙盒代码主机服务太忙而无法处理请求。

参数是否有问题,或者我应该将实际创建委托给工作流?

回答

0

以下试用这个: public override void ItemAdding(SPItemEventProperties properties) { base.ItemAdding(properties);

  // Get the web where the event was raised 
      SPWeb spCurrentSite = properties.OpenWeb(); 

      //Get the name of the list where the event was raised   
      String curListName = properties.ListTitle; 

      //If the list is our list named SubSites the create a new subsite directly below the current site 
      if (curListName == "SubSites") 
      { 
       //Get the SPListItem object that raised the event 
       SPListItem curItem = properties.ListItem; 
       //Get the Title field from this item. This will be the name of our new subsite 
       String curItemSiteName = properties.AfterProperties["Title"].ToString(); 
       //Get the Description field from this item. This will be the description for our new subsite 
       string curItemDescription = properties.AfterProperties["Description"].ToString(); 
       //Update the SiteUrl field of the item, this is the URL of our new subsite 
       properties.AfterProperties["SiteUrl"] = spCurrentSite.Url + "/" + curItemSiteName; 

       //Create the subsite based on the template from the Solution Gallery 
       SPWeb newSite = spCurrentSite.Webs.Add(curItemSiteName, curItemSiteName, curItemDescription, Convert.ToUInt16(1033), "{8FCAD92C-EF01-4127-A0B6-23008C67BA26}#1TestProject", false, false); 
       //Set the new subsite to inherit it's top navigation from the parent site, Usefalse if you do not want this. 
       newSite.Navigation.UseShared = true; 
       newSite.Close(); 


      } 
     } 
+0

那么,它退出更快,“语言不支持在此服务器上”:) 将该参数更改为currentWeb.Language给出同样的错误;我做的另一个改变是使用OpenWeb()而不是仅仅获取Web属性,并将创建的调用放入_using_语句中。还尝试使用字符串“STS#0”,其文档状态是团队网站的字符串值... – 2012-02-17 13:09:17

0

似乎是一些死锁情况;我通过使用事件ItemAdded来解决我的特殊情况(从AfterProperties中的设置值更改为更新ListItem)。在那里,由于某种原因,Webs.Add()的呼叫正常完成...

+0

嗨, 我面临同样的问题。你能展示最终结果如何? Webs.Add()调用开始,但从未结束。查看SP Designer中的子网站列表向我显示了一个包含网址但没有标题和内容的网站。 我的代码与此非常相似:http://sharepoint247.com/sharepoint2010/sharepoint-2010-event-handler-to-create-subsites/ – miracules 2013-02-28 13:15:40

+0

对不起,该代码已被SPSite.SelfServiceCreateSite( ),以避免用户运行代码的问题(由Microsoft推荐)。请注意,对于这种情况,您需要传递两个用户,这两个用户将成为主要和次要网站集管理员。 此外,发现如果使用RunWithElevatedPrivileges(),则需要为SPSite等创建新对象,而不是重复使用在该代码之外创建的对象。 – 2013-05-15 11:05:07

相关问题