先需要将自定义的站点模版从网站集转移到Farm中。
找一个自己已经完成配置及设计的网站,在网站设置里面选择另存为模版。要注意的是不是所有的站点类型都有另存为模版的功能。
存完之后可在解决方案库的界面里面看到刚刚保存的模版,点击下载到本地
下载到本地之后用VisualStudio创建项目中的Import Solution Package项目模版创建项目并打开此文件
打开项目中的Feature3,然后将Scope选择为Farm,其他不需要改。
然后发布即可
执行Powershell语句,由于我的模版中有一个自定义的Feature所以还需要部署一个dll所以命令后面加了-GACDeployment
然后就可以在管理中心的解决方案管理里看到上传的解决方案
同时,创建站点集中也可以看见自定义的模版了。
最后代码实现
protected bool CreateSiteCollection() { try { SPSecurity.RunWithElevatedPrivileges( delegate { // Provide the Web Application URL under which all site collection and sub sites needs to be provisioned string webApplicationUrl = "http://win-7k7cq3pbmd1"; /* --------------------------------------------------------------------------------------- */ // For Creating HR Site Collection string relativeUrl = "/sites/"+txtSiteTitle.Text; string siteTitle = txtSiteTitle.Text; string siteDescription = "WElcome to "+txtSiteTitle.Text+" Site Collection"; uint languageCode = 2052; // 1033 is the code for english string siteTemplate = txttemplatename.Text;//"BLANKINTERNET#0"; string primaryAdmin = @"dingweipingAdministrator"; // Domain followed by Network Id/Alias string secondaryAdmin = @"dingweipingAdministrator"; //Domain followed by Network Id/Alias. If Secondary Admin is not required then assign Null /* --------------------------------------------------------------------------------------- */ SPWebApplication objWebApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl)); using (SPSite mySiteCollection = objWebApplication.Sites.Add(relativeUrl, siteTitle, siteDescription, languageCode, siteTemplate, primaryAdmin, string.Empty, string.Empty, secondaryAdmin, string.Empty, string.Empty)) { /* --------------------------------------------------------------------------------------- */ // For Creating Benifits Sub Site inside HR Site Collection //string subSiteTitle = "Benifits"; //string subSiteDescription = "Employee Benifits"; //string subSiteRelativeUrl = "/sites/hr/benifits"; // Benifits Sub Site will get added to the HR site collection /* --------------------------------------------------------------------------------------- */ //using (SPWeb web = mySiteCollection.AllWebs.Add(subSiteRelativeUrl, subSiteTitle, subSiteDescription, languageCode, siteTemplate, false, false)) ; } }); return true; } catch (Exception ex) { lblstatus.Text = ex.Message; return false; } }