internal void ApplayTemplate(SPWebTemplate template)
        {
            if (SiteCollection == null)
            {
                return;
            }
            SPWeb _root = SiteCollection.RootWeb;

            _root.ApplyWebTemplate(template);
        }
        public override void Provision(SPWebProvisioningProperties props)
        {
            SPWeb  eCaseRootWeb  = props.Web;
            SPSite eCaseRootSite = eCaseRootWeb.Site;

            eCaseRootWeb.ApplyWebTemplate("SusDebRootWeb");

            #region Create & Configure the eCase FAST Search Site
            SPWebTemplate eCaseFastSrchCtrSiteDef = eCaseRootSite.GetWebTemplates(1033)["SusDebFastSearchSiteDef#0"];
            using (SPWeb eCaseFastSrchCtr = eCaseRootWeb.Webs.Add(ECASE_SRCH_CTR_URL, ECASE_SRCH_CTR_TITLE, ECASE_SRCH_CTR_DESC, 1033, eCaseFastSrchCtrSiteDef, false, false))
            {
                // Display Ribbon by default
                eCaseFastSrchCtr.AllProperties["__DisplayShowHideRibbonActionId"] = false.ToString();

                #region Configure Site Collection Search Center settings
                eCaseRootWeb.AllProperties["SRCH_ENH_FTR_URL"]         = eCaseFastSrchCtr.Url + "/pages";
                eCaseRootWeb.AllProperties["SRCH_SITE_DROPDOWN_MODE"]  = "ShowDD";
                eCaseRootWeb.AllProperties["SRCH_TRAGET_RESULTS_PAGE"] = eCaseFastSrchCtr.Url + "/pages/results.aspx";
                eCaseRootWeb.Update();
                #endregion

                #region Set MasterPage
                try
                {
                    // Get the masterpage
                    SPFile eCaseSrchMaster = eCaseRootWeb.GetFile(eCaseRootWeb.Url + "/_catalogs/masterpage/eCase_minimal.master");
                    // eCaseFastSrchCtr.MasterUrl = eCaseSrchMaster.ServerRelativeUrl; /* DO NOT APPLY TO SYSTEM PAGES */
                    eCaseFastSrchCtr.CustomMasterUrl = eCaseSrchMaster.ServerRelativeUrl;
                    eCaseFastSrchCtr.Update();
                }
                catch (Exception x)
                {
                    Logger.Instance.Error(string.Format("Failed to set master page in SusDeb FAST Search Center at {0}", eCaseFastSrchCtr.Url),
                                          x, DiagnosticsCategories.eCaseSearch);
                }
                #endregion

                #region Populate Search Tabs Lists
                try
                {
                    #region Create SearchResults Tabs List
                    Guid srchResultsGuid = eCaseFastSrchCtr.Lists.Add("Tabs in Search Results", "Use this list to store the tabs displayed in search results.",
                                                                      "SearchResults", "285dfda8-ae65-4ac1-8f6a-39ff7187bba9", 301, "100", SPListTemplate.QuickLaunchOptions.Off);
                    SPList     srchResultsList = eCaseFastSrchCtr.Lists[srchResultsGuid];
                    SPListItem resultsAspx     = srchResultsList.AddItem();
                    resultsAspx[eCaseConstants.FieldGuids.TABS_LIST_TAB_NAME] = "All Sites";
                    resultsAspx[eCaseConstants.FieldGuids.TABS_LIST_PAGE]     = "results.aspx";
                    resultsAspx[eCaseConstants.FieldGuids.TABS_LIST_TOOLTIP]  = "Click for results from All Sites";
                    resultsAspx.Update();
                    SPListItem peopleResultsAspx = srchResultsList.AddItem();
                    peopleResultsAspx[eCaseConstants.FieldGuids.TABS_LIST_TAB_NAME] = "People";
                    peopleResultsAspx[eCaseConstants.FieldGuids.TABS_LIST_PAGE]     = "peopleresults.aspx";
                    peopleResultsAspx[eCaseConstants.FieldGuids.TABS_LIST_TOOLTIP]  = "Click for people results";
                    peopleResultsAspx.Update();
                    #endregion

                    #region Create SearchCenter Tabs List
                    Guid srchCenterGuid = eCaseFastSrchCtr.Lists.Add("Tabs in Search Pages", "Use this list to store the tabs displayed in the default blank search pages.",
                                                                     "SearchCenter", "285dfda8-ae65-4ac1-8f6a-39ff7187bba9", 301, "100", SPListTemplate.QuickLaunchOptions.Off);
                    SPList     srchCenterList = eCaseFastSrchCtr.Lists[srchCenterGuid];
                    SPListItem defaultAspx    = srchCenterList.AddItem();
                    defaultAspx[eCaseConstants.FieldGuids.TABS_LIST_TAB_NAME] = "All Sites";
                    defaultAspx[eCaseConstants.FieldGuids.TABS_LIST_PAGE]     = "default.aspx";
                    defaultAspx[eCaseConstants.FieldGuids.TABS_LIST_TOOLTIP]  = "Click for results from All Sites";
                    defaultAspx.Update();
                    SPListItem advancedAspx = srchCenterList.AddItem();
                    advancedAspx[eCaseConstants.FieldGuids.TABS_LIST_TAB_NAME] = "All Sites";
                    advancedAspx[eCaseConstants.FieldGuids.TABS_LIST_PAGE]     = "advanced.aspx";
                    advancedAspx[eCaseConstants.FieldGuids.TABS_LIST_TOOLTIP]  = "Click for results from All Sites";
                    advancedAspx.Update();
                    SPListItem peopleAspx = srchCenterList.AddItem();
                    peopleAspx[eCaseConstants.FieldGuids.TABS_LIST_TAB_NAME] = "People";
                    peopleAspx[eCaseConstants.FieldGuids.TABS_LIST_PAGE]     = "people.aspx";
                    peopleAspx[eCaseConstants.FieldGuids.TABS_LIST_TOOLTIP]  = "Click for people results";
                    peopleAspx.Update();
                    #endregion
                }
                catch (Exception x)
                {
                    Logger.Instance.Error(string.Format("Failed to create Tabs lists in eCase FAST Search Center at {0}", eCaseFastSrchCtr.Url), x, DiagnosticsCategories.eCaseSearch);
                }
                #endregion
            }
            #endregion

            #region Create a Daily Schedule for the UpdateNextDueDate Timer Job
            string jobName = string.Format(UPDATE_NEXT_DUE_DATE_TIMER_JOB_NAME, eCaseRootSite.ServerRelativeUrl);
            foreach (SPJobDefinition job in eCaseRootSite.WebApplication.JobDefinitions)
            {
                if (job.Name == jobName)
                {
                    job.Delete();
                }
            }

            // Install the job.
            UpdateNextDueDateTimerJob unddTimerJob = new UpdateNextDueDateTimerJob(jobName, eCaseRootSite.WebApplication, null, SPJobLockType.Job, eCaseRootSite.ID.ToString());

            SPDailySchedule schedule = new SPDailySchedule();
            schedule.BeginHour    = 1;
            schedule.EndHour      = 2;
            unddTimerJob.Schedule = schedule;
            unddTimerJob.Update();

            #endregion
        }
        private void ApplyWebTemplate(SPWeb web, string templateName)
        {
            SPWebTemplateCollection templates = web.GetAvailableWebTemplates(GetLocale(web));
            var template = (from SPWebTemplate t in templates
                            where t.Title == templateName
                            select t).FirstOrDefault();

            if (template != null)
            {
                try
                {
                    web.ApplyWebTemplate(template);
                }
                catch (SPException exception)
                {
                    /*logger.Debug(exception.NativeErrorMessage);*/
                }
            }
        }
        private string createSite(string url, SPSiteSubscription subscription, string title, string description, string user, string fullName, string email, string template, out Guid siteid, bool useHostHeader)
        {
            siteid = Guid.Empty;
            string errors = "";

            try
            {
                SPSite site;
                if (subscription != null)
                {
                    site = app.Sites.Add(subscription, url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader);
                }
                else
                {
                    site = app.Sites.Add(url, title, description, 1033, "", user, fullName, email, null, null, null, useHostHeader);
                }
                try
                {                                 //SPSite site = spApp.Sites.Add(bUrl + url, user, email);
                    site.AllowUnsafeUpdates = true;
                    using (SPWeb web = site.OpenWeb())

                    {
                        web.Title = title;
                        web.AllowUnsafeUpdates              = true;
                        web.Site.AllowUnsafeUpdates         = true;
                        web.Site.RootWeb.AllowUnsafeUpdates = true;
                        //SPDocumentLibrary solGallery1 = (SPDocumentLibrary)web.Site.RootWeb.Site.GetCatalog(SPListTemplateType.SolutionCatalog);


                        string[] files   = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\workengine\\templates\\" + template);
                        int      counter = 1;
                        foreach (string file in files)
                        {
                            addfile(Path.GetFileNameWithoutExtension(file), web, file, counter);
                            counter++;
                        }

                        SPWebTemplate webtemplate = null;
                        foreach (SPWebTemplate t in web.GetAvailableWebTemplates((uint)web.Locale.LCID))
                        {
                            if (t.Title == template)
                            {
                                webtemplate = t;
                                break;
                            }
                        }

                        if (webtemplate != null)
                        {
                            web.ApplyWebTemplate(webtemplate);
                        }

                        //web.Update();

                        //web.AllUsers.Add(user, email, fullName, "");
                        ////web.AllUsers.Add(System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), "", System.Configuration.ConfigurationManager.AppSettings["owner"].ToString(), "");
                        //web.Users[user].Name = fullName;
                        //web.Users[user].Update();

                        //web.Update();
                        SPUser owner = web.AllUsers[user];


                        web.SiteGroups.Add("Administrators", owner, owner, "");

                        //web.Update();
                        web.AssociatedOwnerGroup = GetSiteGroup(web, "Administrators");
                        SPRole roll = web.Roles["Full Control"];
                        roll.AddGroup(web.SiteGroups["Administrators"]);
                        SPMember newOwner = web.SiteGroups["Administrators"];

                        web.SiteGroups.Add("Team Members", newOwner, owner, "");
                        web.SiteGroups.Add("Visitors", newOwner, owner, "");
                        web.SiteGroups.Add("Project Managers", newOwner, owner, "");
                        //web.Update();

                        web.AssociatedVisitorGroup = GetSiteGroup(web, "Visitors");
                        web.AssociatedOwnerGroup   = GetSiteGroup(web, "Administrators");
                        web.AssociatedMemberGroup  = GetSiteGroup(web, "Team Members");
                        // web.Update();

                        //web.SiteGroups["Administrators"].Users[user].Name = fullName;
                        //web.SiteGroups["Project Managers"].Users[user].Name = fullName;
                        //web.SiteGroups["Team Members"].Users[user].Name = fullName;
                        //web.SiteGroups["Visitors"].Users[user].Name = fullName;

                        web.Roles.Add("Contribute2", "Can view, add, update, delete and manage subwebs", web.Roles["Contribute"].PermissionMask | SPRights.ManageSubwebs);

                        roll = web.Roles["Full Control"];
                        roll.AddGroup(web.SiteGroups["Administrators"]);

                        roll = web.Roles["Contribute"];
                        roll.AddGroup(web.SiteGroups["Team Members"]);

                        roll = web.Roles["Read"];
                        roll.AddGroup(web.SiteGroups["Visitors"]);

                        roll = web.Roles["Contribute2"];
                        roll.AddGroup(web.SiteGroups["Project Managers"]);

                        siteid = site.ID;

                        if (txtDatabaseServer.Text != "")
                        {
                            errors = mapReports(site);
                        }
                    }
                }
                catch (Exception ex) { errors = ex.ToString(); }
                finally
                {
                    if (site != null)
                    {
                        site.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                errors = ex.Message;
            }

            return(errors);
        }