在ASP中创建SharePoint站点时找不到自定义模板

时间:2011-03-15 09:35:13

标签: asp.net sharepoint-2010

我正在尝试使用ASPX代码隐藏从自定义模板以编程方式创建SharePoint网站。这是代码,连接到触发操作的按钮的事件处理程序:

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

问题是,我正在寻找的模板确实是通过调用parent.GetAvailableWebTemplates()找到的:它的模板名称是“{54833785-C62D-48E0-9DC7-1D458BB60814} #prosjektrom“,它的标题是”prosjektrom“。但是,我得到了例外

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

这对我来说似乎很奇怪,因为模板绝对是可用的模板之一。有谁知道发生了什么事?

2 个答案:

答案 0 :(得分:1)

你可以尝试这个功能...... 对我来说很好用!

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

答案 1 :(得分:0)

我设法使这件事成功,但解决问题的方法并不完全明显。更改了从

中查找模板的循环
foreach (SPWebTemplate thisTemplate in parent.GetAvailableWebTemplates(localeID))

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

还在模板规范中将“template.Name”更改为“template”。但这不应该造成任何差别。