Types of Features
Excerpt by Doug Ware
Most of the elements in a SharePoint installation are either defined using features or leverage features in some way. Accordingly, there are feature types that define most of the site elements with which you are already familiar.
Feature/Site Template Association
Feature/site template associations allow you to associate new features and functionality with existing site definitions or site templates so that, when you provision a site, SharePoint automatically activates the features. This technique is known as feature stapling. Feature stapling makes versioning much easier because it allows you to associate new features with preexisting site definitions. Stapled features also provide an excellent way to handle situations that require activation code, because they are applied after their associated site is fully provisioned.
Two features are involved in feature stapling: the feature that contains the functionality you want to add to an existing site template, and the feature/site template association feature. Feature/site template associations have Farm or WebApplication scope. You can globally associate a feature with every site definition on a farm. Associate a feature with the global site definition by specifying GLOBAL for the TemplateName attribute. Whenever you create a site, the global site definition is applied, so your feature is also applied.
Use a workflow feature to attach a custom workflow to SharePoint. The feature includes the code for the workflow and the necessary forms for each step.
Document converters automatically convert a document library item from one format to another. WSS does not offer document converters, but MOSS includes several (for example, to convert documents to HTML) and third-party converters to support conversion to PDF and other formats.
Site Columns and Content Types
In features, you define site columns with a Field element. All of the core site columns are defined in a feature named fields that is activated by the global site definition. You can use the fields feature as a reference when creating your own site columns or content types. Just as the global site definition activates the fields feature for site columns, the ctypes feature is also activated globally for content types. You can create a new content type with a feature using the ContentType feature element. Sometimes, you will need to associate a content type with an existing list or alist that another feature created as part of a larger solution. Use the ContentTypeBinding feature type for that task.
You can define a new type of list-including content type associations, views, and custom forms-with a ListTemplate feature. Each of the standard lists and libraries is defined by a ListTemplate feature element. For example, the DocumentLibrary feature defines the Document Library template. You can create new list instances by using ListInstance to specify the target URL and the list template that defines the list. SharePoint exposes a rich event model for lists that allows custom code to run whenever something happens to a list or an item. Use the Receivers feature type to associate the assembly and a class that contains a handler for a specific event type to all lists defined by a particular list template.
The Module feature allows you to deploy files to a site. You can use it to add files to a library or simply place the files onto the site in a specified folder. In the latter case, the module creates the folder if necessary.
You might expect the Control feature to install controls, but it doesn't. Many of the core site pages use a Web control named DelegateControl. DelegateControl is a placeholder; when the page renders, it loads a concrete control instance that is based on an ID and a sequence number. DelegateControl looks for the lowest sequence number of a given ID and loads the associated control. Individual Control features define the IDs and sequence numbers. This feature allows site owners to override existing controls with their own specialized instances on existing sites, without altering the sites or their definitions. A common use of this technique is to replace the basic search control, which is defined by a DelegateControl on the master page with an ID of SmallSearchInputBox. You can replace this control when you brand a site by deploying your own control or Web Part and then creating a Control feature with the SmallSearchInputBox identifier and a low sequence number.
Just as Control allows you to insert controls in defined placeholders without changing the target site or pages, the Custom Actions feature allows you to insert or hide links on pages and menus. CustomAction and HideCustomAction both require that you specify a location, and they affect the resulting page without directly altering its definition. Use Custom Actions to show or hide different elements of the site, including the contents of many drop-down menus such as site actions, personal settings, list settings, and item edit menus.
You can also use Custom Actions to show and hide links on the Site Settings page and on the Create Libraries, Lists, And Sites page. Custom actions enable greater control of additional navigation within the site than you can accomplish with the top-level menus. You can use custom actions to display an option on the edit item menu of a list or document library's items. For example, you could display a link to a function that merges a contact item's data with a document template to create a form letter.
New Feature Elements in 2010
SharePoint 2010 adds a number of feature elements to the schema.
The PropertyBag element allows you to add properties to items in a site. The Save Site as Template functionality uses PropertyBag to create solutions that can duplicate the source site with high fidelity. When using a PropertyBag to target a file, the use is identical to the Properties child element of the File element within a module. The difference is that the PropertyBag is independent of the need to deploy the file. In other words, you can apply properties to items that already exist.
The WebTemplate element provides an alternative to traditional site templates. In most cases this element is now the preferred method for creating new templates for custom sites.
The WorkflowAssociation allows declarative association of a workflow template to a list, content type, or site. Previously you had to use managed code to create a workflow association. Managed code is still a fully supported option.
The feature schema definition, wss.xsd, includes four other workflow elements that are currently undocumented and unused by any of the built-in features that come with SharePoint 2010. They are:
- Workflow Actions
- Web Part Adder Extension
- User Migrator
- Group Migrator
This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.