Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4094724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:46:58+00:00 2026-05-20T19:46:58+00:00

I’m trying to programatically create a SharePoint site from a custom template using ASPX

  • 0

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:

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

/// <summary>
/// Look up a site template from site by name and Locale id (1033 = english
/// 1044 = bokmål). Return null if no result.
/// </summary>
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;
}

The thing is, the template I’m looking for is 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

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

This seems really weird to me, as the template is definitely among the available templates. Anyone know what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:46:58+00:00Added an answer on May 20, 2026 at 7:46 pm

    I somehow managed to make this thing work, but it isn’t completely obvious what solved the problem. Changed the loop that looks up the templates from

    foreach (SPWebTemplate thisTemplate in parent.GetAvailableWebTemplates(localeID))
    

    to

    foreach (SPWebTemplate thisTemplate in SPContext.Current.Web.GetAvailableWebTemplates(localeID))
    

    Also changed “template.Name” to “template” in the specification of the template. This shouldn’t cause any difference, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.