10

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?

5 Answers 5

3

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

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; 
        } 
1
  • This looks like another question rather than an answer to the original question. If you need more information try posting a new question with the details. Commented Jun 6, 2013 at 1:16
0

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

SPWebTemplateCollection templates = portal.GetWebTemplates(LCID);
0

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

0

I used the below code to create subsite using CSOM C#

public bool CreateSite(ClientContext ctx,SomeClassWithDetails request) {
            try
            {
                WebCreationInformation wci = new WebCreationInformation();
                wci.Url = "Url of Subsite";
                wci.Title = "subsite title";
                wci.Description = "subsite description";
                wci.UseSamePermissionsAsParentSite = true;
                wci.WebTemplate = "{GUID_OF_TEMPLATE}#TemplateName";
                wci.Language = 1033; //English -1033, French 1036 etc
                Web w = ctx.Site.RootWeb.Webs.Add(wci);
                ctx.ExecuteQuery();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Additionally if you don't know the value of the template, you can get if by visiting the below url

https://sharepointsite/_api/web/GetAvailableWebTemplates(1033)

1033 is for English, 1036 is for french and so on

Search with the name of the template and you have to use the value inside <d:Name> tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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