1

I have a "project list" (title, lead, members, site-URL) that is supposed to refer to team sites under the site that has the project list. So I added an SPItemEventReceiverto my feature in a sandbox solution to do that.

In ItemAdding(properties), I invoke the following:

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

But when debugging, the call to Add throws an SPException wrapping a COMException for a HResult code of FAILED with the message The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.

Is there something wrong with the parameters, or should I delegate the actual creation to a workflow instead?

2 Answers2

0

Following try with this: 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();


           }
      }
Rony SP
  • 2,618
  • 15
  • 16
  • Well, it exited faster with "The language is not supported on this server" :) Changing that parameter to currentWeb.Language gives the same error; the other change I did was using OpenWeb() instead of just getting the Web property, and putting the creating call into a _using_ statement. Also tried with the string "STS#0" which the docs state is the string value for a team site... – Tor Iver Wilhelmsen Feb 17 '12 at 13:09
0

Seems to be some deadlock situation; I solved my particular case by using the post-event ItemAdded instead (changing from setting values in AfterProperties to updating the ListItem instead). There, for some reason, the call to Webs.Add() completes normally...

  • Hi, I face the same problem. Would you be able to show how the end result was? The Webs.Add() call starts, but never finishes. Looking at the list of subsites in SP Designer shows me a site with a URL but no title and no content. My code is very similar to this: http://sharepoint247.com/sharepoint2010/sharepoint-2010-event-handler-to-create-subsites/ – miracules Feb 28 '13 at 13:15
  • Sorry, that code has been replaced with a call to SPSite.SelfServiceCreateSite() in order to avoid a problem with what user runs the code in question (recommended by Microsoft). Note that for that case you need to pass two users, which will become primary and secondary site collection admins. Also, it was discovered that if you use RunWithElevatedPrivileges() you need to create new objects for SPSite etc. instead of reusing objects created outside that code. – Tor Iver Wilhelmsen May 15 '13 at 11:05