Feature receiver in sharepoint

In this article I will describe that how to write custom feature receiver.
This is very helpful when we are required to perform some action while activating feature.
To explain this functionality In this I will modify site title on feature activation and remove site title on feature deactivation.

Detail:-

Create your usual WSP solution. How to create WSP solution; for this please visit my below article:-
Create web part using WSP solution

Now we will create feature.xml and receiver class file..

1. Feature.xml

we need to create only Feature.xml, (Elements.xml not required in this case) which will use ReceiverAssembly and ReceiverClass attributes, to indicate that there is a source file named AvinashFeatureRNDReceiver.cs.

1
2
3
4
5
6
7
8
 
<?xml  version="1.0"  encoding="utf-8"  ?>
<Feature  Id="write feature id here" Title="HomeProjectRND"
	Description="This feature  demonstrates feature receiver implementation." Version="1.0.0.0"
	Scope="Web" ReceiverAssembly="Home.Project.Solution,  Version=1.0.0.0,  Culture=neutral,
	PublicKeyToken=f2w533b16a1234h" ReceiverClass="Home.Project.Solution.AvinashFeatureRNDReceiver" Hidden="FALSE"
	xmlns="http://schemas.microsoft.com/sharepoint/">
</Feature>

 
2. AvinashFeatureRNDReceiver.cs

This class inherits SPFeatureReceiver and override FeatureActivated and featureDeactivating
methods to update site title.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
using  System;
using  Microsoft.SharePoint;
 
namespace  Home.Project.Solution
{
  public class AvinashFeatureRNDReceiver  : SPFeatureReceiver
  {
 
	public  override void  FeatureInstalled(SPFeatureReceiverProperties properties){}
	public  override void  FeatureUninstalling(SPFeatureReceiverProperties properties){}
	public  override void  FeatureActivated(SPFeatureReceiverProperties properties)
	{
		SPWeb   web  =  (SPWeb)properties.Feature.Parent;
		web.Title  =  "Site Modified by Avinash";
		web.Description  =  "Avinash RND  Feature activated.";
		web.AllowUnsafeUpdates  =  true;
		web.Update();
		web.AllowUnsafeUpdates  =  false;
	}
 
	public  override void  FeatureDeactivating(SPFeatureReceiverProperties properties)
	{
		SPWeb   web  =  (SPWeb)properties.Feature.Parent;
		web.Description  =  "Avinash RND  Feature deactivated";
		web.AllowUnsafeUpdates  =  true;
		web.Update();
		web.AllowUnsafeUpdates  =  false;
	}
  }
}

Hope it helps.

Thanks!
Avinash

calendarApril 22, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , ,  · Posted in: MOSS, SharePoint

Leave a Reply

Spam Protection: , required

myworldmaps infoyen