Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to create a site from my template...

     [WebMethod]
    public ResponseObject addGroup()//AddGroup addGroupObj)
    {

        try
        {
            string fullsite = SPContext.Current.Web.Url + "/sites/Reply Corp";

            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                using (SPSite site = new SPSite(fullsite))
                                {
                                    site.AllowUnsafeUpdates = true;

                                    SPWeb newSite = site.AllWebs.Add("RELATIVE", "TITOLO", "DESCRIZIONE", 1033, "Sfera.wsp", false, false); //1033 = English
                                }
                            });
            return new ResponseObject() { SPResultStatus = ResponseObjectSPResultStatus.SUCCESS };
        }
        catch (Exception exx)
        {
            return new ResponseObject() { Message = exx.Message, ExceptionType = exx.GetType().ToString(), Stacktrace =exx.StackTrace, SPResultStatus = ResponseObjectSPResultStatus.FAILURE,  };
        }

    }

This code created my site but generate the exception that couldn't find Sfera template.. The template exists, i try to put it as "Sfera" and as "Sfera.wsp" but without success..

Is therte another way to create programmatically a site on a custom template??

Thank you very much!

share|improve this question
    
I think you have to specify the template id (like STS#1 for team site), not the wsp file that contains the template. Look at msdn.microsoft.com/en-us/library/ms473439.aspx –  Steve B Feb 6 '12 at 10:23
    
Ok, i've already read it... But i want to create it like my template, not like STW#1.. By "graphic" UI is possibile, i hope it is also by code! –  Ziba Leah Feb 6 '12 at 10:30

5 Answers 5

up vote 5 down vote accepted

You can find the correct template and use it in code like this (templateName is the title of your saved template, locale is the locale ID of the template):

// Find correct template
SPWebTemplateCollection templates =
    SPContext.Current.Site.GetWebTemplates(Convert.ToUInt32(locale)); // or GetAvailableWebTemplates or GetCustomWebTemplates

SPWebTemplate siteTemplate = null;

foreach (SPWebTemplate template in templates)
{
    if (template.Title.Equals(templateName))
    {
        siteTemplate = template;
        break;
    }
}

Then you can use that SPWebTemplate as a parameter like this:

parentWeb.Webs.Add(
projectUrl,
projectName,
"",
Convert.ToUInt32(locale),
siteTemplate,
true, // True breaks inheritance
false)

When you save site as a template, you can find its ID from (being on site collection root) Site Actions -> Site Settings -> Solutions. There you can click on the ROW of the solution, highlighting it and from ribbon Solutions -> Dectivate, from the dialog you can see more details of the template/solution.

And finally, for the available languages, you can get them using

SPWeb.RegionalSettings.InstalledLanguages

More detailed example on how to loop through all languages here.

share|improve this answer
    
When i go to Site Actions -> Site Settings -> Solutions there is no id.. Only Name, also when i click on Edit Item! May I use that Name as ID? I forget to tell I'm on Foundation Version.. I don't know if this make some difference! –  Ziba Leah Feb 6 '12 at 12:23
1  
If you follow my code example, you can use the Name as ID. If the Name doesn't work, please use the Template name you gave whan saving the template in the first place (can't remember which one it was just now). –  Jussi Palo Feb 6 '12 at 14:36
    
Thank you very much.. The problem is that if i put 1033 as language (Eng-USA) it doesn't work.. neither with Italian and these are the only 2 languages on my server! So i can't find by code my template! –  Ziba Leah Feb 6 '12 at 15:11
1  
Try it with GetAvailableWebTemplates or GetCustomWebTemplates instead of GetWebTemplates. –  Jussi Palo Feb 6 '12 at 15:28

Instead of "Sfera.wsp", it should be the name of the Template. Now, the name of the Template is in the following format: "{FeatureID}#TemplateTitle"

Where FeatureID is the Id of the feature which is deploying your site template and TemplateTitle is the specified Title of the Template.

Example:

SPWeb newSite = site.AllWebs.Add("RELATIVE", "TITOLO", "DESCRIZIONE", 1033, "{B77C383F-2EFC-40a7-8477-114A59D6F088}#SferaTemplateTitle", false, false);
share|improve this answer

Sfera.wsp is a solution package file, not a template. In order to make this work you'll need the Template ID.

I don't know how you created your site template, but I would recommend creating a Site Definition via Visual Studio and deploy it to your site, as it will provide you with all the necessary information (IDs, Names etc.) you'll need in your code to be able to create a site programmatically.

share|improve this answer
    
I've created it by "Save as template" from UI! Where can i find the ID now? thank you! –  Ziba Leah Feb 6 '12 at 11:31
    
Please see my answer. –  Jussi Palo Feb 6 '12 at 11:52
    
@ZibaLeah Take a look at JussiPalo's example. That's a way to get the template by using the name. –  Daniel Ziga Feb 6 '12 at 12:21

I'd use something like below. This tries getting Web templates, then Site Definitions. It may be faster to iterate.

public static SPWebTemplate GetTemplate(SPWeb web, string templateName, int locale)
{
    SPWebTemplate tmpl;
    try
    {
        tmpl = web.Site.GetCustomWebTemplates(locale)[templateName];
    }
    catch (ArgumentException)
    {
        try
        {
            tmpl = web.GetAvailableWebTemplates(locale)[templateName];
        }
        catch (ArgumentException)
        {
            throw new SPException(string.Format("Could not find template '{0}'", templateName));
        }

    }
    return tmpl;
}
share|improve this answer
    
How can i know the template locale? If i put English US (1033) the template didn't appear! –  Ziba Leah Feb 6 '12 at 13:46

Here is the code I use to do this and it always works:

using (SPSite site = new SPSite("http://rootweb"))
        {

            SPWeb web = site.OpenWeb("Subsiste");

            SPWebTemplate SiteTemplate = null;

            foreach (SPWebTemplate wt in web.GetAvailableWebTemplates((uint)1053)) // 1053 is locale code (language of site), for EN-US use LCID 1033, Sweden 1053 http://msdn.microsoft.com/en-us/goglobal/bb964664
            {

                if (wt.Title == "Template_Name_123") 
                {
                    SiteTemplate = wt;
                    break;
                }

            }

            if (SiteTemplate != null)
            {
                string SiteURL = "project_123";
                string SiteTitle = "Project 123 Site";
                string SiteDescription = "Site for Project 123!";
                SPWeb newWeb = web.Webs.Add(SiteURL, SiteTitle, SiteDescription, (uint)1053, SiteTemplate, false, false); // 1053 is locale code (language of site), for EN-US use LCID 1033, Sweden 1053 http://msdn.microsoft.com/en-us/goglobal/bb964664

                //the code that does something with created site goes here, "newWeb."! examples: (etc)
                //newWeb.Navigation.UseShared = true;
                //newWeb.BreakRoleInheritance(false);
            }

        } //using rootweb
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.