0
votes

To get standard templates I do:

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

I thought I could do:

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

To get the custom templates but the dropdown is empty, what am I doing wrong here?

Thanks in advance.

Edit: the templates are activated in the solutions gallery.

2

2 Answers

0
votes

I got it to work with

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
votes

SPSite does not contain a GetAvailableWebTemplates method. For those who would like to use the code use the one below. So I have added this line of code:

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

Full code:

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