Sharepoint:从项目事件处理程序创建子网站

时间:2012-02-17 09:49:17

标签: sharepoint sharepoint-2010 spweb

我有一个“项目列表”(标题,主角,成员,网站网址),应该引用具有项目列表的网站下的团队网站。所以我在沙盒解决方案中为我的功能添加了SPItemEventReceiver来做到这一点。

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);

但是在调试时,对Add的调用会抛出一个SPException包含一个错误的HResult代码的错误消息沙箱代码执行请求被拒绝,因为沙盒代码主机服务太忙了处理请求。

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

2 个答案:

答案 0 :(得分:0)

试试这个:     public override void ItemAdding(SPItemEventProperties properties)           {               base.ItemAdding(属性);

          // 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();


           }
      }

答案 1 :(得分:0)

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