Geir Smestad
Geir Smestad

Reputation: 1394

Custom template not found when creating SharePoint site in ASP

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?

Upvotes: 0

Views: 3641

Answers (2)

Mathieu Levesque
Mathieu Levesque

Reputation: 11

You can try this function ... works fine for me !

private String getUniqueTemplateID(String strTemplateName, SPSite site)
        {
            String strUniqueID = "";

            SPWebTemplate webTemplate = site.GetWebTemplates(1033).Cast<SPWebTemplate>().FirstOrDefault(wt => wt.Title == strTemplateName);

            strUniqueID = webTemplate.Name;

            return strUniqueID;
        }

Upvotes: 1

Geir Smestad
Geir Smestad

Reputation: 1394

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.

Upvotes: 0

Related Questions