Saturday, February 25, 2012

Creating the subsite's automaticallty by choose custom template in Sharepoint 2010

In Sharepoint 2010 no option avilabe to save all subsite to site template uisng save as template option like moss 2007, we can achive this to create feature for  WebProvisioned Event to create subsite in site creation it self in sharepoint 2010..



public override void WebProvisioned(SPWebEventProperties properties)
       {
           SPWeb currentWeb= properties.Web as SPWeb;
           // check  the selected custom template
           CreateSubsites(currentWeb);
       }

Creating Subsite
-----------------

 public static void CreateSubsites(SPWeb web)
       {
           Hashtable templatewebInformation = new Hashtable();
           templatewebInformation .Add("URL", "siteurl");
           templatewebInformation .Add("TemplateName", "template name");
           templatewebInformation .Add("Title", "site title");
           templatewebInformation .Add("Description", "site Description");
           templatewebInformation .Add("LCID", "1033");
           SPWeb temlateNewWeb = CreateSite(web, templatewebInformation );

          Hashtable templatewebInformation1 = new Hashtable();
           templatewebInformation1 .Add("URL", "siteurl1");
           templatewebInformation1 .Add("TemplateName", "template name1");
           templatewebInformation1 .Add("Title", "site title1");
           templatewebInformation1 .Add("Description", "site Description1");
           templatewebInformation1 .Add("LCID", "1033");
           SPWeb temlateNewWeb = CreateSite(web, templatewebInformation1 );
         }

Creating the Site
------------------
public static SPWeb CreateSite(SPWeb web, Hashtable webInformation)
       {
           SPWeb newWeb = null;
           if (webInformation["TemplateName"] == null)
               return newWeb;
           SPDocumentLibrary solutions = (SPDocumentLibrary)web.Site.GetCatalog(SPListTemplateType.SolutionCatalog);
           // Get Web Template
           SPWebTemplateCollection webTemplates = web.Site.RootWeb.GetAvailableWebTemplates(1033);
           SPWebTemplate webTemplate = null;
           foreach (SPWebTemplate item in webTemplates)
           {
               if (item.Name.Contains(webInformation["TemplateName"].ToString()))
               {
                   webTemplate = item;
               }
           }
           if (webTemplate != null)
           {
               if (webInformation["URL"] == null)
                   return newWeb;
               string URL = webInformation["URL"].ToString().Trim();
               if (string.IsNullOrEmpty(URL))
                   return newWeb;
               string title = webInformation["Title"] == null ? URL : webInformation["Title"].ToString();
               string description = (webInformation["Description"] == null) ? string.Empty : webInformation["Description"].ToString();
               uint LCID = (webInformation["LCID"] == null) ? 1033 : Convert.ToUInt32(webInformation["LCID"]);
               bool useUniquePermissions = false;
               bool bConvertIfThere = false;

               using (SPWeb subWeb = web.Webs.Add(URL, title, description, LCID, webTemplate, useUniquePermissions, bConvertIfThere))
               {
                   subWeb.AllowAllWebTemplates();
                   subWeb.Navigation.UseShared = true;
                   newWeb = subWeb;
               }
           }
           return newWeb;
       }

No comments:

Post a Comment