Share this page

Monday, November 16, 2009

How to create a feature in SharePoint and to develop the events in Visual Studio 2008

In this first article I want to share the way that we can create a feature in Sharepoint 2007 and to develop the events fired when it’s installed. To create this project I’m using the last version of WSPBuilder from Codeplex.
The first task is to create a project using the template installed by WSPBuilder installer. The project must be a project to create a blank feature. Once the project was created we are going to add a new item. This item must be based in the template BlankFeature, one of the templates installed by WSPBuilder. The project in Visual Studio 2008 should be as we can see in Image 1.

[Image 1]
image

Ok, the next step will be to create a class (FeatureEvents) in the root of the project as we can see in Image 1 and it must be inherit of SPFeatureReceiver, class of Sharepoint that. This class has 4 events that we need develop. In Section 2 we can see the name of events and a brief description.

[Section 1]

1) FeatureActivated: This event will be fired when the feature was activated.
2) FeatureDeactivating: This event will be fired when the feature is Deactivating
3) FeatureInstalled: This event will be fired when the feature was installed
4) FeatureUnistalling: This event will be fired when the feature was uninstalling

The last step in modified the XML feature.xml to declare the feature, our assembly and our class where the events are developed. In Section 2 we can see the code for the feature.xml.

[Section 3]

<Feature  Id="e3954709-8993-417e-b8e8-9a5b3bca44aa"
          Title="FeatureEventsFired"
          Description="FeatureEventsFired"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
          ReceiverAssembly="Siderys.Blog.Feature.FeatureEvents,Version=1.0.0.0, Culture=neutral, PublicKeyToken=711eed342842acee"
          ReceiverClass="Siderys.Blog.Feature.FeatureEvents.FeatureEventsClass"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>  
  </ElementManifests>
</Feature>

Once installed our package, the events will be fired and we catch it in our class.

Download Source Code
image

Fabián Imaz

No comments: