Step 1: Let’s create a project ‘SharePointFeature’. You can find the source code attached with this article. Below is the project tree which has two XML files and a class file which will process the four events:
FeatureInstalled
, FeaturesUnInstalling
, FeatureActivated
,
and FeatureDeactivating
.Let’s understand these three files first.
- Feature.XML: This is the most important file because it helps SharePoint identify the feature. All features in SharePoint are identified by the GUID key.
<Feature Id="48DEF2C4-33F9-4885-B0DE-6FE82E9FDCD8"
Title="Go to Custom Pages"
Description="This features enables us to goto Custom Page"
Scope="Web"
Hidden="FALSE"
ImageUrl="menuprofile.gif"
ReceiverAssembly="SharePointFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=af83741e324f585c"
ReceiverClass="SharePointFeature.clsFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="ElementManifest.xml" />
</ElementManifests>
</Feature>
To generate a new GUID, click on Tools -> Create GUID and click
‘New GUID’. The Tools menu can be got from within the IDE. We have
marked the GUID value
which you will need to copy and paste in the feature.xml file.Other than feature description and title, there are two important things in the XML file. First, it points towards some other XML file, and second, it points to an assembly which captures events.
SiteActionsToolBar
. This tool bar is the one which you see on the right hand side corner of your SharePoint portal. There is also
a URLaction
property which specifies which URL it redirects to.<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="SiteActionsToolbar"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="10"
Title="Display Custom Pages"
Description="This links helps to display Custom Page"
ImageUrl="_layouts/SharePoint2/menuprofile.gif">
<UrlAction Url="_layouts/SimplePageCodeBehind.aspx"/>
</CustomAction>
</Elements>
In other words, ElementManifest.xml specifies the location of the feature and which page it should redirect to.SharePoint
namespace as shown in the below code snippet.using System;
using System.Collections.Generic;
using System.Text;
// Refer the SharePoint namespace
using Microsoft.SharePoint;
We need to implement the SPFeatureReceiver
class and implement all the events.namespace SharePointFeature
{
// Inherit from the 'SPFeatureReceiver" class
public class clsFeatureReceiver : SPFeatureReceiver
{
// Implement the four events of SPFeatureReceiver class
public override void FeatureInstalled(SPFeatureReceiverProperties properties){}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
// This event fires when the feature is activated
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
....
....
....
....
}
// This event fires when the feature is deactivated
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
....
....
....
...
}
}
}
As a sample, in the FeatureActivated
event, we have set the description and title of the website.public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// get the object of SharePoint Web
SPWeb site = (SPWeb)properties.Feature.Parent;
// Set the description ,properties , titile and update the SpWeb object
site.Description = "Click on the SiteActions to See how the custom page display";
site.Properties["OriginalTitle"] = "Display CustomPage";
site.Properties.Update();
site.Title = "This Site now has the custom page display";
site.Update();
}
In FeatureDeactivating
, we have reverted back to the title and description.public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// Get hold of the SharePoint web object and reset back the values
SPWeb site = (SPWeb)properties.Feature.Parent;
site.Description = "Custom Page display is disabled";
site.Title = site.Properties["OriginalTitle"];
site.Update();
}
>cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
To install the feature, run the below command using STSADM. Please
note that you need to specify the relative directory path and not the
physical path
of the Feature.xml file.>stsadm -o installfeature -filename DisplayCustomPage\Feature.xml
To ensure that SharePoint registers this feature, run IISRESET on the machine.Now you can see your feature enabled in the site actions menu. If you click on the feature, i.e., ‘Display Custom Pages’, it will redirect you to SimplePageCodeBehind.aspx.
The other point to be noted is that the events have fired and set the title and description as described in the code.
Try to experiment and deactivate the feature and you will see the title and description changing.
0 comments:
Post a Comment