Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top