How to Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010
Site Definitions give you lots of flexibility in choosing custom layouts, features, elements, modules, etc., that are included when a new site is created in SharePoint. Furthermore, Site Definitions make it easier to maintain and upgrade your SharePoint solutions.
How do I create a custom Site Definition, as well as content related to a Site Definition in SharePoint 2010, using the Visual Studio 2010 project template?
Creating a custom site definition using the Visual Studio project template
To create a site template, follow these steps:
At this point, the SharePoint Customization Wizard will appear (See Figure 2).
In WebTemp_DemoSiteDefs.xml, add our custom site definition into the catalog of site definitions so that we can pick the site definition to provision from the Create Site and Workspace application pages. It is important that the Template Name exactly matches our site definition name. This file name begins with the "webtemp" prefix as required by SharePoint. The general format of WebTemp_DemoSiteDefs.xml is as shown in the following figure.
Figure 4When this Site Definition is installed, a global WebTemp_DemoSiteDefs.xml is installed in %Program Files%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LCID\XML, where LCID is the numeric ID of language/culture. Ex: 1033 for English.
Onet.xml is all about Navigation areas, List Definitions, Modules, List Templates, Configurations, BaseTypes, Components, etc. (See Figure 5).
Figure 5When this Site Definition is installed, a global Onet.xml is installed in %Program Files%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\GLOBAL\XML.
For each Site Definition, there is a corresponding Onet.xml installed in a sub-directory (template name) at %Program Files%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates.
You can see more details about the format of Onet.xml file here: http://msdn.microsoft.com/en-us/library/ms474369.aspx.
Note: Once deployed, a Site definition cannot be modified. You should create a new kind of site. This can be done by creating a new WebTemp*.xml and Onet.xml.
Open the newly created site, and you'll see a custom site Web page derived from your site definition.
Incorporating a custom master page into the Site Definition
Figure: 10When we create a master page module, Visual Studio 2010 generates a new feature called Feature1.feature. We need to change properties and apply the master page feature to our Site Definition. Visual Studio 2010 creates a separate feature for master page instead of depending on the Site Definition so that it can be operated independent of our Site Definition.
Click to Feature1.feature. In the Feature Design mode we can edit title, description, and scope of feature.
Figure: 11In the Manifest mode, you also see title, ID, scope, and element files of this feature (See Figure 12).
Figure: 12Next, we need to add this custom master page to the Site Definition.
Update onet.xml file:
Add our master pages feature (which ID="23d9ad40-56e7-4473-af17-c8c7fbc39b99") under WebFeatures. If our master page feature had been a "Site" scoped feature we would specify it under the SiteFeatures section (See Figure 13).
Update the default configuration and specify that our configuration should use our custom master page by setting the CustomMasterUrl and MasterUrl points to our custom master page (living in the master page gallery). By setting these values, we are effectively telling SharePoint that whenever .aspx refers to custom.master page, replace that with meetingworkspace.master page which is our custom master page (See Figure 13).
Creating custom fields and adding them to the site definition
Define custom fields that will be used to create a custom list later.
These fields provide additional data required by the code in the Site
To define custom fields:
When adding a custom field, you must specify the ID, Type, Name, DisplayName, and Group attributes for the field in order for it to appear correctly in SharePoint. The statement completion erroneously indicates that you need only the first three of these attributes when, in fact, all five are required.
When we add an Empty Element, Visual Studio auto-generates Feature2 for this instance of Elements, so we need to change the default properties of Feature2. I changed title to Custom Contact Field Feature (scope is Site).
Next step, update onet.xml file:
Add the Custom Contact Field feature under SiteFeatures in onet.xml file (See Figure 16).
Figure 16Press F5 to run and deploy the project.
At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings. Under the Galleries section of the Site Settings page, click the Site columns link.
In the Site Column Gallery page, scroll the page down to the Custom Contact heading and note the new field item has been added, Region and Standard Rate (See Figure 17).
Creating a content type that includes the custom fields
To add a content type
Figure 18Note: When I added the Contact content type, by default, Visual Studio added it to the Custom Contact Field feature (it will be deployed to Site scope). You can create a new feature for this content type if you wish.
Press F5 to run and deploy the project.
Creating a list definition based on the content type and adding it to the site definition
Creating a list definition and list instance that will use the new content type and field.
To create a list definition and list instance
Figure 20To add a field to the list definition
Adding default list data to a list instance
To provide an instance of the new list definition and some default
employee information to the DemoSiteDefs - ContactListDefinition list
after the site definition is deployed.
Creating a visual Web Part and adding to the Site Definition
We will create a visual Web Part to display on the Site Definition's main page.
To create a visual Web Part
Figure 30This line associates the name DemoVisualWebPart with the Web Part and its code. The Namespace parameter is the same as the namespace used in the DemoVisualWebPartUsercontrol.ascx code file.
Our final solution structure should now look like the following figure.
Figure 33All done! Our solution is now ready for packaging into a .wsp solution file and deployment into the SharePoint farm.
You can download a demo project here.
I hope that you found it helpful to learn how to create a more full-featured Site Definition, incorporating several features and elements that you might wish to add to the Site Definition.