Download
project at here
How to create Site Collections from a Custom Web Template Programming
Open
Visual Studio 2010.
Go to
File => New => Project.
Select
Empty SharePoint Project template from the installed templates.
Enter
the Name “CreateSiteCollectionFromTemplate” and click Ok.
Right
click on the solution and click on Add a new item.
Select
the VisualWebPart template from the installed template.
Enter
the Name as CreateSiteCollectionFromTemplate and click Ok.
Now the
entire solution looks like following.
Open CreateSiteCollectionFromTemplateUserControl.ascx then
drag and drop button follows as:
Double click to button and add reference:
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.IO;
using System.Linq;
Code in button Button1_Click
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.IO;
using System.Linq;
namespace
CreateSiteCollectionFromTemplate.CreateSiteCollectionFromTemplate
{
public partial class CreateSiteCollectionFromTemplateUserControl
: UserControl
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
SPWebApplication spWebApplication = SPWebApplication.Lookup(new
Uri("http://devsharepoint/"));
using (SPSite
spSite = spWebApplication.Sites.Add("/sites/HomeTemplate",
"SharePoint", null, 1033, null, "administrator", "Administrator", "quochung211187@gmail.com"))
{
SPWeb rootWeb = spSite.RootWeb;
// Get Solutions Gallery
SPDocumentLibrary spDocumentLibrary =
(SPDocumentLibrary)spSite.GetCatalog(SPListTemplateType.SolutionCatalog);
// Upload Solution File with the Web Template
(You must copy HomeTemplate.wsp to C:\Windows)
SPFile solutionFile =
spDocumentLibrary.RootFolder.Files.Add("HomeTemplate.wsp",
File.ReadAllBytes(@"..\..\HomeTemplate.wsp"));
// Activate Solution
SPUserSolution spUserSolution =
spSite.Solutions.Add(solutionFile.Item.ID);
// Activate Features
Guid solutionId =
spUserSolution.SolutionId;
// Activate Site Collection Features
SPFeatureDefinitionCollection
spFeatureDefinitionCollection = spSite.FeatureDefinitions;
var features = from
SPFeatureDefinition spFeatureDefinition
in spFeatureDefinitionCollection
where spFeatureDefinition.SolutionId.Equals(solutionId)
&& spFeatureDefinition.Scope == SPFeatureScope.Site
select spFeatureDefinition;
foreach (SPFeatureDefinition
feature in features)
{
spSite.Features.Add(feature.Id, false, SPFeatureDefinitionScope.Site);
}
// Get Web Template
SPWebTemplateCollection
spWebTemplateCollection = spSite.RootWeb.GetAvailableWebTemplates(1033);
SPWebTemplate webTemplate = (from SPWebTemplate
spWebTemplate
in spWebTemplateCollection
where spWebTemplate.Title == "HomeTemplate"
select
spWebTemplate).FirstOrDefault();
if (webTemplate != null)
{
spSite.RootWeb.ApplyWebTemplate(webTemplate.Name);
}
}
}
}
}
Deploy Project
Go to central Admin create new Site
collection then save that site collection to template and set name is “HomeTemplate.wsp”, download that template and save at C:\Windows Folder
Go to the site where you have deployed the
solution. Add webpart
Click button, automatically site collection is created
follows as:
Check in Central Admin, see site collection is created by go to Central Admin => Application Management => View all
site collections
Einfach wunderbar! Es hat mir viel geholfen. Cam On.
ReplyDelete