2011-01-27 65 views
0

自定義模板要獲得標準的模板,我做的:獲取編程的SharePoint 2010

private void getTemplates() 
{ 
    string server = serverURL(); 
    using (SPSite siteCollection = new SPSite(server)) 
    { 
     SPWebTemplateCollection Templates = siteCollection.GetWebTemplates(1033); 
     foreach (SPWebTemplate template in Templates) 
     { 
       ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name)); 
     } 
    } 
} 

我想我可以這樣做:

private void getTemplates() 
{ 
    string server = serverURL(); 
    using (SPSite siteCollection = new SPSite(server)) 
    { 
     SPWebTemplateCollection Templates = siteCollection.GetCustomWebTemplates(1033); 
     foreach (SPCustomWebTemplate template in Templates) 
     { 
       ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name)); 
     } 
    } 
} 

要獲得自定義模板,但下拉列表是空的,我是什麼在這裏做錯了嗎?

在此先感謝。

編輯:模板在解決方案庫中激活。

回答

0

我得到了它與

private void getTemplates() 
{ 
    string server = serverURL(); 
    using (SPSite siteCollection = new SPSite(server)) 
    { 
     SPWebTemplateCollection Templates = siteCollection.GetAvailableWebTemplates(1033); 
     foreach (SPCustomWebTemplate template in Templates) 
     { 
//this gives me all templates, both standard and custom so I filter by name 
if(template.name.ToUpper().StartsWith("CUSTOM")) 
{ 
       ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name)); 
} 
     } 
    } 
} 
0

SPSite的工作不包含GetAvailableWebTemplates方法。對於那些想要使用該代碼的人,請使用下面的代碼。所以我加入了這行代碼:

using(SPWeb web = siteCollection.OpenWeb()) 
    { 
       SPWebTemplateCollection Templates = web.GetAvailableWebTemplates(1033); 

全碼:

private void getTemplates() 
    { 
     string server = serverURL(); 
     using (SPSite siteCollection = new SPSite(server)) 
     { 
using(SPWeb web = siteCollection.OpenWeb()) 
{ 
      SPWebTemplateCollection Templates = web.GetAvailableWebTemplates(1033); 
      foreach (SPCustomWebTemplate template in Templates) 
      { 
    //this gives me all templates, both standard and custom so I filter by name 
    if(template.name.ToUpper().StartsWith("CUSTOM")) 
    { 
        ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name)); 
    } 
} 
      } 
     } 
    } 
相關問題