Search This Blog

2011-02-05

Create A Simple Feature

Step 1-Create A class library project "MyFeature" in C#
Step 2-Add reference "Windows Sharepoint Services"
Step 3-Create a folder "Features" under the class library and a a folder "MyFeature" under the "Features" folder
Step 4-Add a XML file "Feature.xml" under "MyFeature" folder
Step 5-Add a schema file to your XML from the following path-
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\wss.xsd
Step 6-The content of the feature.xml will be as follows
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="0222472A-0CF1-46bc-8B65-3F319C7B3DBE"
Title="My Feature"
Description="This is my first feature"
Hidden="false"
Scope="Web"
ImageUrl="menuprofile.gif"
ReceiverClass="MyFeature.MyFeatureReceiver"
ReceiverAssembly="MyFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d"
>
<ElementManifests >
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
</Feature>


The Id Value must be unique and will be generated from Tools->Guid Generator".Title ,description and image will be displayed in the feature list of your web site.Scope can be Farm,WebApplication,Site,Web depending on the scenario.
ReceiverClass and ReceiverAssembly will be explain in the next section.
ElementManifests element will call another xml.

Step 6-Add "Elements.xml" under "MyFeature" folder.
The content of the Elements.xml will be as follows-
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="MyModule" Path="" Url="">
<File Url="MyModule.aspx"
Type="Ghostable"
IgnoreIfAlreadyExists="FALSE">
</File>
</Module>
<CustomAction Id="SiteActionsToolbar"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1000"
Title="Go To MyModule"
ImageUrl="~/_layouts/images/menuprofile.gif">
<UrlAction Url="~site/MyModule.aspx"/>
</CustomAction>
</Elements>

CustomAction element defines where the feature will be displayed after activation.
UrlAction element defines the url of the aspx page.But to access the url we first need to define File url.

Step 7-Add a text file "MyModule.aspx" under the "MyFeature" folder.The content will be as follows
<%@ Page Language="C#" MasterPageFile="~masterurl\default.master" %>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<h2>Hello , This is my test page</h2>
</asp:Content>

Step 8-Add a class file under the class library
"MyFeatureReceiver.cs"


The content will be as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace MyFeature
{
public class MyFeatureReceiver:SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb site = (SPWeb)properties.Feature.Parent;
// SPWeb site = SPContext.Current.Web;//when the scope of the feature is site
// track original site Title using SPWeb property bag
site.Properties["OriginalTitle"] = site.Title;
site.Properties.Update();
// update site title
site.Title = "My Feature has been activated";
site.Update();
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// reset site Title back to its original value
SPWeb site = (SPWeb)properties.Feature.Parent;
//SPWeb site = SPContext.Current.Web;//when the scope of the feature is site
site.Title = site.Properties["OriginalTitle"];
site.Update();

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
}


Step 9-Add a strong name to your class library ,build it and copy the dll to GAC
Step 10-Open the Feature.xml and write the ReceiverClass,ReceiverAssembly according to your "MyFeatureReceiver.cs"

Your solution structure will be as follows-


Step 11-Copy your "MyFeature" folder (which is under the "Features" folder) to the following location
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

Step 12-Install your feature by the following command in the command prompt-
stsadm -o InstallFeature -name MyFeature -force

Stsadm exe will be found in the following path
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\STSADM.EXE

Step 13-Activate your feature from the site setting->site features


Step 14-After activation , you will find the title and label under the menu


Step 15-Click on the "Go To MyModule" ,you will be navigated to your MyModule.aspx



Step 16-After deactivation of your feature ,all of these thing will be undone.

No comments: