Web-scoped and Site-scoped Features can also be deployed via site definitions. Site definitions form the basis from which to create SharePoint sites. For example, the default Team Site template is created from the default STS site definition. You can create your own custom site definitions to define specific functionality for sites, such as custom lists or content types, and you can add that functionality using Features.
In Windows SharePoint Services 2.0, a major advantage of creating site definitions as opposed to effecting change using FrontPage 2003 was the fact that sites provisioned from site definitions remained ghosted. With ghosted site definitions, the pages and templates in those sites were called directly from the Web front-end server rather than an unghosted version in the database, which resulted from FrontPage 2003 modifications. This resulted in better, overall server performance and end user experience.
Although ghosting and unghosting still exist in SharePoint Server 2007, you can now more easily reghost pages after they have become unghosted through customizations using SharePoint Designer 2007. But an added advantage to working with site definitions in SharePoint Server 2007 is the ability to more easily apply robust functionality using Features, to both new and existing site definitions. Features deployed via a site definition also become automatically activated on sites provisioned from the site definition. This removes the need for any manual or programmatic intervention to separately activate Features after Feature installation.
The terminology for ghosting and unghosting has changed for SharePoint Server 2007. In SharePoint Server 2007, ghosting is now referred to as uncustomized and unghosting is now referred to as customized.
This section covers how to create a custom site definition and include Web and Site-scoped Features to add a custom document library and content types and columns.
When sites are deployed based on a site definition containing your custom Features, those Features are automatically activated. However, you still need to have installed those Features before provisioning them as part of your site definition. If you attempt to create a site from a site definition containing a Feature where that Feature has not been installed, you will receive the following error during site creation: "Feature 'GUID' is not installed in this farm, and cannot be added to this scope."
To demonstrate including and deploying Features as part of a site definition, you'll create a custom site definition named Projects which will be based on the default STS site definition. The default site definitions are located on the Web front-end server, at the following location:
%SystemDrive%\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates
Avoid modifying the default site definitions, such as the default STS site definition. Changes made to the default site definitions are not supported by Microsoft, and you risk losing those changes during future upgrades, including service pack upgrades.
To create the new Projects site definition, complete the following steps.
|On the CD|| |
The Projects site definition is included on the book's CD in the\Code\Chapter 26 folder.
In the SiteTemplates directory, locate the STS site definition folder. Copy and paste that folder to the SiteTemplates directory and rename it to Projects.
Register the new Projects site definition by creating a Webtemp.xml, or template, file for the site definition. The Webtemp.xml file notifies SharePoint of the site definition and also enables you to configure additional information for the site definition, such as whether the site definition will be hidden or visible in the template picker when creating new sites, a description for the site definition and custom image to be displayed in the template picker when creating new sites. The default Webtemp.xml files are located at the following location:
%SystemDrive%\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML
The site definition template files must begin with Webtemp for SharePoint to recognize those files as site definition template files.
To create a Webtemp.xml file for the Projects site definition, create a new file named WebTempProject.xml and include the following code. The ID property in the Template Name attribute should be set to above 1000 to avoid any conflict with the default SharePoint templates. The Template Name value must exactly match the name of the site definition. You can also choose to include a DisplayCategory property as part of the configuration. The DisplayCategory value will be displayed as an additional category from which to choose templates when creating sites-for example, in addition to the default categories of Collaboration, Meetings, and Enterprise.
<?xml version="1.0" encoding="utf-8" ?> <!-- _lc _version="12.0.4017" _dal="1" --> <!-- _LocalBinding --> <Templates xmlns:ows="Microsoft SharePoint"> <Template Name="PROJECTS" > <Configuration Title="Projects Site" Hidden="FALSE" ImageUrl="/_layouts/images/stsprev.png" Description="This template creates a site for Project teams to create, organize, and share information quickly and easily. It includes a Document Library, and basic lists such as Announcements, Calendar, Contacts, and Quick Links." DisplayCategory="Projects" > </Configuration> </Template> </Templates>
Save the WebTempProject.xml file, and then perform an IISRESET to deploy the new site definition and make it available as a template in the Template Selection section when creating new sites.
Any changes to the site definition .xml file and related template .xml file necessitate an IISRESET to have those changes implemented.
You can customize sites provisioned from a new site definition by adding in new or custom Features. In this section, you'll add two custom Features to the new Projects site definition; one a Site-scoped Feature, including a ProjectXMeetings content type and columns, and the other a Web-scoped Feature, including a custom Project document library which also includes a custom Resource file.
Each SharePoint site definition includes an ONET.XML file, located in the XML folder under the site definition folder:
%SystemDrive%\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\<sitedefinitionfoldername> \xml
The ONET.XML file includes functional directives for the site definition, including navigation, templates and views, references to default List Features, and custom Features, which will be provisioned to sites created from the site definition. You can include both Site Collection-scoped Features and Web-scoped Features in the ONET.XML file.
To reference a custom Feature in the Projects site definition ONET.XML file and have that Feature deployed and activated in sites provisioned from the Projects site definition, complete the following steps:
|On the CD|| |
The following example assumes you have installed the ProjectContentTypes and ProjectDocumentLibrary Features included on the CD in the\Code\Chapter 26 folder in the Visual Studio SharePoint solution. It also assumes you have copied the Resource file, projectdocumentlibrary.en-US.resx, also included on the CD, to the Resources folder on your SharePoint server.
Using Visual Studio or Notepad, open the Projects site definition ONET.XML file and scroll to the section of the file that includes the <SiteFeatures> and <WebFeatures> tags, and the default Features, as shown in the following example:
<SiteFeatures> <!-- BasicWebParts Feature --> <Feature ID="" /> </SiteFeatures> <WebFeatures> <!-- TeamCollab Feature --> <Feature ID="" /> </WebFeatures>
In this example, because you copied the ONET.XML file from the default STS site definition, the <SiteFeatures> and <WebFeatures> sections are already populated with the default BasicWebParts and TeamCollab Features. You can choose to remove these or leave them if you want to maintain the core functionality offered as part of the STS (Team Site) site definition. The <SiteFeatures> section denotes site collection Features, while the <WebFeatures> section denotes Web Features.
Add a new line to the SiteFeatures section and include the reference to the custom ProjectContentTypes Feature, as shown below:
<SiteFeatures> <!-- BasicWebParts Feature --> <Feature ID="" /> <! - - ProjectContentTypes - -> <Feature ID="" </SiteFeatures>
In the case of the ProjectContentTypes Feature, we need to add this Feature to the SiteFeatures section of the ONET.XML file because the Feature includes the Content Type and Field Element which cannot be scoped to Web.
Next, add a new line to the WebFeatures section and include the reference to the custom ProjectDocumentLibrary Feature, as shown below:
<WebFeatures> <!-- TeamCollab Feature --> <Feature ID="" /> <!- - ProjectDocumentLibrary - -> <Feature ID=""> </WebFeatures>
Save the Projects ONET.XML file and then open the Windows command-line tool, and run an IISRESET.
The Projects site definition now includes two custom Features. When you provision a new site collection based on the Projects site definition, a new content type named ProjectXMeetings will be included in the Site Content Type Gallery of the root site of the site collection. If you click on the ProjectXMeetings content type you'll see that it includes two custom columns, Attendees and Highlights, as defined in the ProjectContentTypes Feature.
The custom Content Type Feature will have been activated during site provisioning but will not be visible on the site's Site Features administration page because this Feature is a hidden Feature, denoted by the Hidden="TRUE" attribute in the ProjectContentTypes Feature.xml file. The custom ProjectDocumentLibrary Feature will also be included on any new sites provisioned from the Projects site definition, as an additional Libraries template named Project Documents. The ProjectDocumentLibrary Feature will be also be visible on the Site Features administration page, which means you can optionally deactivate this Feature following deployment.
The Feature activation will fail if you included a Feature in your site definition that is dependent on another Feature being activated and that Feature is not activated. For example, if your site definition includes a Web-scoped Feature that is dependent on a Site Collection-scoped Feature being activated and the Site Collection-scoped Feature is not activated, the Web-scoped Feature activation will fail when deployed via the site definition.
If you remove a Feature reference or tag from the ONET.XML file, new sites provisioned from that site definition will continue to include an activated instance of the Feature until you perform an IISRESET. Once you've performed an IISRESET, after removal of the Feature reference from the ONET.XML file, the Feature remains installed and active on any existing sites created from that site definition as long as the Feature has not been deactivated or uninstalled.
Any sites created from the site definition subsequent to the removal of the Feature reference from ONET.XML and following an IISRESET still include the Feature if the Feature remains installed and scoped for the instance of the sites provisioned. But in this case, the Feature will not be automatically activated. You need to activate the Feature separately, either using the command-line tool or through the administrative user interface page, Site Features.
If you uninstall a Feature where the Feature is referenced in a site definition, the Feature will be uninstalled from all existing sites including those sites created from the site definition. Attempting to provision sites from that same site definition, which still contains the reference to the uninstalled Feature, generates the following error message: "Feature 'GUID' is not installed in this farm, and cannot be added to this scope."
If you subsequently reinstall the same Feature, it will be reinstalled to any existing sites provisioned from the site definition but will not be activated. Any new sites created from the site definition containing a reference to the Feature, once the Feature is reinstalled, will be automatically activated.