4

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!

2
  • 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
    Commented Feb 6, 2012 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
    Commented Feb 6, 2012 at 10:30

5 Answers 5

6

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.

4
  • 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
    Commented Feb 6, 2012 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
    Commented Feb 6, 2012 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
    Commented Feb 6, 2012 at 15:11
  • 1
    Try it with GetAvailableWebTemplates or GetCustomWebTemplates instead of GetWebTemplates.
    – Jussi Palo
    Commented Feb 6, 2012 at 15:28
4

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

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.

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

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;
}
1
  • How can i know the template locale? If i put English US (1033) the template didn't appear!
    – Ziba Leah
    Commented Feb 6, 2012 at 13:46
0

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

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.