Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to programmatically create a site using custom web templates in sharepoint 2010.

In 2007 I used to get a

SPWebTemplate template = site.GetCustomWebTemplates(LCID)[number];

and used this SPWebTemplate in

SPWeb newSite = web.Webs.Add(url, title, description, LCID, template, true, false);

This worked quite well for 2007. But in 2010 the site templates are saved into the solution store

SPSite.Solutions

which only returns SPUserSolution objects and not SPWebTemplates.

So my question is: How can I programmatically create sites using the site templates from the solution store.


UPDATE: I tried to use

SPWeb newSite = web.Webs.Add(url, title, desc, LCID, templateName, true, false);

where templateName is a string like '{Template-GUID}#TemplateName', but it still does not work. It throws the following error:

Critical error: File or arguments not valid for site template '{4a2d8952-a0af-49a6-8f55-46a59c8d68a2}#CustomerSiteTemplate'. Parameter name: WebTemplate 

Any other ideas how to create a site based on a SPWebTemplate?

share|improve this question

5 Answers

Thanks for the post. But using the method GetTemplate(string solutionName, SPSite siteCollection) above was able to get the Template Name, but since in my SPWebTemplateCollection collection, I have multiple site templates with the same "Title". so using the "Title" does not return me the correct site template I need. For example, I have the site template called "hosts" first, then I removed it, recreate a new site template called it with the title "hosts" again. So my problem is, when the new site was created, it was using the old "hosts" template instead of the new one, any one can help me?

string templateFromMethod = GetTemplate("hosts", siteInUserContext);

SPWeb newSubWeb = subsites.Add(TextBox1.Text, TextBox1.Text, TextBox1.Text, 1033, templateFromMethod, false, false);

private static String GetTemplate(string solutionName, SPSite siteCollection) 
        { 
            string templateName = null; 
            //SPWebTemplateCollection coll = siteCollection.GetWebTemplates(1033); 
            SPWebTemplateCollection coll = siteCollection.GetCustomWebTemplates(1033); 
            foreach (SPWebTemplate template in coll) 
            {

                if (template.Title.Equals(solutionName, StringComparison.CurrentCultureIgnoreCase))
                {
                    templateName = template.Name;
                } 
            } 
            return templateName; 
        } 
share|improve this answer

http://blog.mastykarz.nl/programmatically-creating-sites-site-collections-custom-web-template/

share|improve this answer
Thx for your answer, but that is not exactly what i look for. I don't want to create site collections, instead I want to create sites. In this example on line 28-31 the author queries for a SPWebTemplate object. My Problem is that when I save a Site as Template, these templates are stored in the solution store. I can not find these templates in the GetAvailableWebTemplates collection thus I have no SPWebTemplate object for Webs.Add() call. – sebuseba Aug 27 '10 at 13:43

Here is how to do it with a custom 2010 Sharepoint WebTemaple definition:

<?xml version="1.0" encoding="utf-8"?>
  <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <WebTemplate
    Name="CustomWebTemplate"
    BaseTemplateName="STS"
    BaseTemplateID="1"
    BaseConfigurationID="0"
    Title="Custom Site Template Title"
    Description=""
    DisplayCategory="Custom Template" />
  </Elements>

First find custom templates "Title"

private static String GetTemplate(string solutionName, SPSite siteCollection)
    {
        string templateName = null;
        SPWebTemplateCollection coll = siteCollection.GetWebTemplates(1033);

        foreach (SPWebTemplate template in coll)
        {
            if (template.Title.Equals("Custom Site Template Title", StringComparison.CurrentCultureIgnoreCase))
            {
                templateName = template.Name;
            }
        }

        return templateName;
    }

Then create site:

  private static void AddSite(SPWeb parentSite, string name, string templateName)
    {
        //Create the new site from the template
        bool allowUnsafeupdates = parentSite.AllowUnsafeUpdates;
        parentSite.AllowUnsafeUpdates = true;

        SPWeb newSite = parentSite.Webs.Add(name, name, name, 1033, templateName, false, false);
        newSite.Update();

        parentSite.AllowUnsafeUpdates = allowUnsafeupdates;
    }
share|improve this answer
up vote 0 down vote accepted

finally i found a place/property where my previously saved site templates are stored:

SPWebTemplateCollection templates = portal.GetWebTemplates(LCID);
share|improve this answer

This is how I did it:

Extract from my blog:

Please Note that siteUrl below contains the URL for the site collection, for example, Site_Name or sites/Site_Name. It may either be server-relative or absolute for typical sites.

protected void CreateTeamSite(string siteUrl, string newsiteName, SPFieldUserValue siteOwner)
{
    try
    {
        using (SPSite site = new SPSite(“http://SPSite”))
        {
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;

                // Set the user for your new site
                string ownerLogin = siteOwner.User.LoginName; //user’s login name
                string ownerEmail = siteOwner.User.Email;
                string ownerName = siteOwner.User.Name;

                //Create new web site
                SPWeb newWeb = siteCollection.Add(“/” + siteUrl, siteName, “site description”, 1033,{Name of the Site Template}, ownerLogin,ownerName,ownerEmail );
                newWeb.Update();
            }
        }
}

To get Name of the existing site template {Name of the Site Template} follow the below steps

  1. Save a Site as a Template.

  2. Go to Site settings –> sites & workspaces –> create.

  3. IE Tools > Developer Tools > Find > Select Element By Click > View > Source > DOM (Element) > Highlight and copy the section

    test

The one in bold will be your site template name.

source

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.