Search This Blog

Saturday, January 24, 2015

How to create Web Template in SharePoint 2010 using Feature

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
  1. Open Visual Studio 2010
  2. Click File -> New -> Project
  3. Select “Empty SharePoint Project” under SharePoint -> 2010 category.
  4. Enter Name for the project e.g. SampleWebTemplate.
  5. Click OK.
  6. Select “Deploy as a sandboxed solution” in next window.
  7. Ensure local site is your targeted site.
  8. Click “Finish
Create Element with Feature
  1. Right click on project (i.e. SampleWebTemplate) points to “Add” and click “NewItem
  2. Select “Empty Element” under SharePoint -> 2010 category and name it to SWTSite (Sample Web Template Site)
  3. Above step will create a SharePoint element (Elements.xml) with a feature associated with it.
  4. 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.
  5. To rename feature, Click on feature1 and Press F2.
  6. Change feature name from Feature1 to “SWTSite”
  7. Open “Elements.xml” Now we need to add few tags in it as shown below.
  8. Add element “WebTemplate”. WebTemplate element has four mandatory attributes,Name (Internal name of site template), BaseTemplateIDBaseTemplateName andBaseConfigurationID are used to define base template on which new template is based on.
  9. Title and DisplayCategory are used for ease. DisplayCategory is tab name under which template will be visible.
  10. 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>
  11. New Web Template is based on an existing web template (which is STS template).
  12. 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.
  13. Open “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts” folder
  14. Copy both “default.aspx” and “defaultdws.aspx” into your project folder.
  15. Copy “ONET.xml” from “sts\xml” folder to your project folder also.
  16. Right click on Element(i.e. SWTSite) points to “Add” and click “Existing Item
  17. Select all three files that you copied before (default.aspxdefaultdws.aspx andonet.xml) and press “Add
  18. Make some changes in feature we must set feature Scope to Farm to be able to do this.
  19. Right-click on SWTSite feature and click “View Designer
  20. Change Scope to “Farm
  21. Change Title/Add description of the feature if you would like to, this will appear under farm features.
  22. As an example change title to “SWTSite Feature” and add description “This feature will add new web template based on STS template.”
  23. Important! Make sure three files (default.aspxdefaultdws.aspx and onet.xml) that we added before are also included in deployment.
  24. To ensure this, Click on the file one by one and select Development Type as “ElementFile” whereas Elements.xml will be of type “ElementManifest
  25. Make sure you mark Sandboxed Solution to FALSE in project properties because sandboxed solution cannot be deployed at Farm level.
  26. 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.
  27. Create new site collection and apply new template.

No comments:

Post a Comment