title: Custom template not found when creating SharePoint site in ASP
body: <p>I'm trying to programatically create a SharePoint site from a custom template using ASPX code-behind. This is the code, connected to an event handler for the button that triggers the action:</p>
  
  <pre><code>protected void Trigger_OnClick(object sender, EventArgs e)
  {
  
  SPSite site = new SPSite("http://portal.innovit.internal/sites/lab/prosjektstyring-prototyp");
  
  using (SPWeb web = site.OpenWeb())
  {
      try
      {
          SPWebTemplate template = getSiteTemplate(web, "prosjektrom", 1044);
  
          // Create new subsite
          web.Webs.Add("templateSubsite3", "Subsite created in VS from template 3", 
                       "What the title says", 1033, template.Name, false, false);
      }
      catch (Exception ex)
      {
          TextOutput.Text = ex.Message;
          StackTrace.Text = ex.StackTrace;
      }
  }
  }
  
  /// &lt;summary&gt;
  /// Look up a site template from site by name and Locale id (1033 = english
  /// 1044 = bokmål). Return null if no result.
  /// &lt;/summary&gt;
  private SPWebTemplate getSiteTemplate(SPWeb parent, String templateName, 
                                        uint localeID)
  {
      // Loop through all available templates
  foreach (SPWebTemplate thisTemplate in parent.GetAvailableWebTemplates(localeID))
  {
      if (thisTemplate.Title.ToLower().Equals(templateName.ToLower()))
      return thisTemplate;
  }
  return null;
  }
  </code></pre>
  
  <p>The thing is, the template I'm looking for <em>is</em> indeed found by the call to parent.GetAvailableWebTemplates(): Its template name is "{54833785-C62D-48E0-9DC7-1D458BB60814}#prosjektrom", and its title is "prosjektrom". However, I get the exception</p>
  
  <pre><code>"File or arguments not valid for site template {54833785-C62D-48E0-9DC7-1D458BB60814}#prosjektrom. Parameter name: WebTemplate"
  at Microsoft.SharePoint.SPWebTemplateCollection.get_Item(String strKey)
  at Microsoft.SharePoint.SPWeb.ApplyWebTemplate(String strWebTemplate)
  at Microsoft.SharePoint.SPWeb.CreateWeb(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere)
  at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)
  at CreateSiteDialog2.Layouts.CreateSiteDialog2.CreateSiteDialog2.Trigger_OnClick(Object sender, EventArgs e)
  </code></pre>
  
  <p>This seems really weird to me, as the template is definitely among the available templates. Anyone know what's going on?</p>