Friday, July 19, 2013

Create site and apply theme programmatically- SharePoint 2013

 Below code will create  a community site with new master page and theme

try
            {
                SPSite site = SPContext.Current.Site;
                             try
                             {
                                 SPWeb newWeb = site.AllWebs.Add(txtTitle.Text, txtTitle.Text, txtDescription.Text, 1033, "COMMUNITY#0", true, false);
                                 newWeb.AllowUnsafeUpdates = true;
                                 newWeb.Update();
                                 newWeb.MasterUrl = "/_catalogs/masterpage/NewMaster.master";
                                 newWeb.AllProperties["__InheritsThemedCssFolderUrl"] = "True";
                                 newWeb.Update();
                                 SPFile file = site.RootWeb.GetFile("/_catalogs/theme/15/Palette012.spcolor");
                                 SPTheme s = SPTheme.Open("S", file);
                                 s.ApplyTo(newWeb, true);
                                 newWeb.AllowUnsafeUpdates = false;
                                 SPUtility.Redirect(site.Url + "/" + txtTitle.Text, SPRedirectFlags.Trusted, Context);
                             }
                             catch (Exception es) { }
                                finally {}
            }

No comments: