Web Templates are introduced in SharePoint 2010 mainly to define site definition as sandboxed solutions. You can create a web template in following manner.
Create SharePoint Project
- Open Visual Studio 2010
- Click File -> New -> Project
- Select “Empty SharePoint Project” under SharePoint -> 2010 category.
- Enter Name for the project e.g. SampleWebTemplate.
- Click OK.
- Select “Deploy as a sandboxed solution” in next window.
- Ensure local site is your targeted site.
- Click “Finish“
Create Element with Feature
- Right click on project (i.e. SampleWebTemplate) points to “Add” and click “NewItem“
- Select “Empty Element” under SharePoint -> 2010 category and name it to SWTSite (Sample Web Template Site)
- Above step will create a SharePoint element (Elements.xml) with a feature associated with it.
- Newly created feature name is “Feature1”, we must rename it and name it same as Element name (i.e. SWTSite) otherwise we must change DeploymentLocation inonet.xml.
- To rename feature, Click on feature1 and Press F2.
- Change feature name from Feature1 to “SWTSite”
- Open “Elements.xml” Now we need to add few tags in it as shown below.
- Add element “WebTemplate”. WebTemplate element has four mandatory attributes,Name (Internal name of site template), BaseTemplateID, BaseTemplateName andBaseConfigurationID are used to define base template on which new template is based on.
- Title and DisplayCategory are used for ease. DisplayCategory is tab name under which template will be visible.
- Now “Elements.xml” will look like this.
<?xml version=“1.0“ encoding=“utf-8“?> <Elements xmlns=“http://schemas.microsoft.com/sharepoint/“> <WebTemplate Name=“SWTSite“ Title=“Sample Web Template Site“ BaseTemplateID=“1“ BaseTemplateName=“STS“ BaseConfigurationID=“0“ DisplayCategory=“SWT sites“ /> </Elements>
- New Web Template is based on an existing web template (which is STS template).
- Now we need to copy template’s files and modify them to change whatever in the template. In this example we will not change anything but just re-use the template as is.
- Open “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts” folder
- Copy both “default.aspx” and “defaultdws.aspx” into your project folder.
- Copy “ONET.xml” from “sts\xml” folder to your project folder also.
- Right click on Element(i.e. SWTSite) points to “Add” and click “Existing Item“
- Select all three files that you copied before (default.aspx, defaultdws.aspx andonet.xml) and press “Add”
- Make some changes in feature we must set feature Scope to Farm to be able to do this.
- Right-click on SWTSite feature and click “View Designer”
- Change Scope to “Farm”
- Change Title/Add description of the feature if you would like to, this will appear under farm features.
- As an example change title to “SWTSite Feature” and add description “This feature will add new web template based on STS template.”
- Important! Make sure three files (default.aspx, defaultdws.aspx and onet.xml) that we added before are also included in deployment.
- To ensure this, Click on the file one by one and select Development Type as “ElementFile” whereas Elements.xml will be of type “ElementManifest”
- Make sure you mark Sandboxed Solution to FALSE in project properties because sandboxed solution cannot be deployed at Farm level.
- Deploy your solution. If successfully deployed go ahead otherwise, look through all steps above if you missed something. If you encountered with something un-addressed, please write it in comments so I can update the post and make it more useful for others.
- Create new site collection and apply new template.
No comments:
Post a Comment