views:

125

answers:

1

Hi,

I have a parent website under which i want to create subsites on the fly using visual studio 2010.

The subsites which i want to create have a common stuructre(in temrs of lists & custom).

So i saved the template in sharepoint 2010,it becomes wsp and goes into soution gallery.

Now how do i use this template say mytemplate.wsp,to create site in visual studio.

i tried this

using (SPSite site = new SPSite ("http://infml01132:5566/sites/VRND " ))

        {

            using (SPWeb web = site.OpenWeb())

            {



                SPWebTemplateCollection myTemplates = site.GetCustomWebTemplates(Convert .ToUInt32(web.Locale.LCID));

                //SPWebTemplateCollection temlates1 = site.GetCustomWebTemplates(1033);

                //SPWebTemplateCollection temlates = site.GetWebTemplates(1033);

                //SPCustomWebTemplate

                SPWebTemplate subsitetemplate = myTemplates["vrndtmpl" ];



                web.Webs.Add("subsite1" , "rnd subsite1" , "subsite description changed" , Convert .ToUInt16(1033), subsitetemplate, false , false );

            }

        }

But i dont get my template in mytemplates collection.

Thanks

A: 

We are using this code to retrieve site template by it's name:

private SPWebTemplate GetSiteTemplate(SPSite site, string templateName, int lcid)
{
    foreach (SPWebTemplate webTemplate in site.GetWebTemplates(lcid))
    {
        if (webTemplate.Name.ToLower().Contains(templateName.ToLower()))
        {
            return webTemplate;
        }
    }
    return null;
}

It is 100% works. So, in your case, you should write:

using (SPSite site = new SPSite ("http://infml01132:5566/sites/VRND"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPWebTemplate subsitetemplate = GetSiteTemplate(site, "vrndtmpl", 1033);
        web.Webs.Add("subsite1" , "rnd subsite1" , "subsite description changed" , uint(1033), subsitetemplate, false , false);
     }
}

Also, please, ensure, that you have deployed your site template under SiteTemplates directory, and the corresponding webtemp.xml file to <14 hive>\TEMPLATE\1033\XML or deployed it through feature, as described here.

omlin
Hey thanks a lot omlin.Your method helped me out , i used getavailablewebtemplates method. :)