using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ManabFeature
{
public class FeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb site = (SPWeb)properties.Feature.Parent;
// track original site Title using SPWeb property bag
site.Properties["OriginalTitle"] = site.Title;
site.Properties.Update();
// update site title
site.Title = "Hello World";
site.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// reset site Title back to its original value
SPWeb site = (SPWeb)properties.Feature.Parent;
site.Title = site.Properties["OriginalTitle"];
site.Update();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
throw new Exception("The method or operation is not implemented.");
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
Also note that this feature has been designed to store the original value of the site Title so that it can be restored whenever the feature is deactivated. This is accomplished by using a persistent property bag scoped to the site that is accessible through an SPWeb object’s Properties collection. Note that many of the objects in the WSS object model have a similar Properties property, which can be used to track name-value pairs using a persistent property bag. WSS handles persisting these named value pairs to the content database and retrieving them on demand.
The next step is to update the feature.xml file with two new attributes so that WSS knows that there are event handlers that should be fired whenever the feature is activated or deactivated. This can be accomplished by adding the ReceiverAssembly attribute and the ReceiverClass attribute, as shown here.
<Feature Id="8684D4A9-1D76-4504-8142-65F5384B1756" Title="Manab Feature" Description="This is my very first custom feature" Scope="Web" Hidden="FALSE" ImageUrl="menuprofile.gif" ReceiverAssembly="ManabFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9d6bb69ab1a4acd7" ReceiverClass="ManabFeature.FeatureReceiver" xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests> <ElementManifest Location="elements.xml" /> </ElementManifests>
</Feature>
The ReceiverAssembly attribute should contain the four-part name of an assembly that has already been installed in the GAC. The ReceiverClass attribute should contain the namespace-qualified name of a public class within the receiver assembly that inherits SPFeatureReceiver.
Once you have made these changes to the feature.xml file, you should be able to test your work. When you rebuild the HelloWorld project, Visual Studio should run the install.bat file to copy the updated version of the feature.xml file to the WSS FEATURES directory and to install the updated version of feature.xml with WSS. The build process should also compile HelloWorld.dll with a strong name and install it in the GAC. Note that you will likely be required to run an IISRESET command to restart the IIS worker process. This is due to the fact that features and assemblies loaded from the GAC are cached by WSS within the IIS worker process.
At this point, you should be able to test your work by activating and deactivating the feature within the context of a WSS site. When you activate the site, it should change the Title of the site to “Hello World.” When you deactivate the feature, it should restore the Title of the site to the original value.
No comments:
Post a Comment