Home > Articles

  • Print
  • + Share This
This chapter is from the book

Introducing SharePoint Features

As indicated earlier features in SharePoint enable you to toggle functionality On or Off. All features created using farm solutions are deployed to a corresponding folder in the <14 Hive>\Template\FEATURES directory. You find a feature.xml file within this directory. Features created using sandboxed solutions get deployed to the database.

You already created a couple of features in this hour in a farm solution and in a sandboxed solution. To see these features activated, go to Site Actions, Site Settings, Site Collection Features under the Site Collection Administration section (see Figure 3.16).

Figure 3.16.

Figure 3.16. Site Collection features

Let’s see more details of the feature.xml file. If not already open, open the HelloWorldFarm project created earlier. Double-click the HelloWorldVWP feature under Features. A designer opens up, as shown in Figure 3.17.

Here you can change the Title, Description, and Scope of the feature. In addition you have the option to remove or add files to your feature. A feature can be scoped to Web, Site, WebApplication or Farm. A Web scoped feature is available only within the site and can be activated by going to Site Actions, Site Settings, Manage Site Features under the Site Actions section. A Site scoped feature is available within the site collection and can be activated by going to Site Actions, Site Settings, Manage Site Collection Features under the Site Collection Administration section. A WebApplication scoped feature usually provides functionality at the web application level. An example is a feature that does modifications to the web.config file. You can activate the WebApplication feature by selecting SharePoint 2010 Central Administration, Application Management, Manage Web Applications. Select a web application and click Manage Features in the ribbon. You will see a pop-up, as shown in Figure 3.18.

Figure 3.17.

Figure 3.17. Visual Studio 2010 Feature Designer

Figure 3.18.

Figure 3.18. Web Application features

Finally, Farm scoped features are activated once in the entire farm. These can again be activated from SharePoint 2010 Central Administration, System Settings, Manage Farm Features. This is shown in Figure 3.19.

Figure 3.19.

Figure 3.19. Farm features

Coming back to the feature designer in the Visual Studio, click Manifest at the bottom of the designer. Here you can see the XML for the feature, as shown in Figure 3.20.

Figure 3.20.

Figure 3.20. Feature Manifest

The id attribute specifies the feature id that uniquely identifies the feature. In addition the ElementManifest attribute specifies the list of files in the feature. Many other elements can be within your feature XML. You find the list of all elements at http://msdn.microsoft.com/en-us/library/ms475601.aspx.

You can see that feature.xml is read-only in this view. You can edit this by expanding the Edit Options at the bottom (see Figure 3.21). Any changes that you make to it will actually happen to HelloWorldVWP.Template.xml, which you can see if you expand the HelloWorldVWP.feature in the Solution Explorer.

Figure 3.21.

Figure 3.21. Feature Manifest editor

It is also important to understand the concept of feature receivers. A feature receiver is a class that inherits from the SPFeatureReceiver base class and handles various events of a feature. To add a feature receiver class, right-click the HelloWorldWP feature and click Add Event Receiver, as shown in Figure 3.22.

Figure 3.22.

Figure 3.22. Adding an event receiver

A new feature receiver is added. Double-click the HelloWorldWP feature and click the manifest to see the feature XML. You can see the following two attributes added to the feature element:

  • ReceiverAssembly="HelloWorldSandBoxed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=96e1896643f851d0
  • ReceiverClass="$SharePoint.Type.abe8d3c9-a062-4a4a-9b4e-d3ec5525d231.FullName$"

This is how the feature receiver class gets associated with the feature. The Receiver class attribute is resolved to the correct name of the class during deployment.

Open the HelloWorldWP.EventReceiver.cs file and you can see various methods that will be commented by default. You can uncomment and use these based on your requirements. Table 3.1 lists descriptions of these functions.

Table 3.1. Events of a SharePoint Feature

Function Name

Description

FeatureActivated

Called when the feature is activated. Use this when you want to perform some action through code on feature activation.

FeatureDeactivating

Called when you deactivate a feature. You usually write code in this to undo the changes that were done in the FeatureActivated event.

FeatureInstalled

Called when the feature is installed

FeatureUninstalling

Called when a feature is uninstalled.

FeatureUpgrading

Newly introduced in SharePoint 2010. This is called when the version of the feature is upgraded.

You work with features in upcoming hours.

  • + Share This
  • 🔖 Save To Your Account