Tuesday, September 29, 2009

Creating SharePoint Features


In this article I’m going to describe how to create SharePoint feature before look at custom web parts . Here I’m going to use Visual Studio 2008 and Visual Studio 2008 extensions for Windows SharePoint Services 3.0 to create my feature.

Creating the Feature

First create new SharePoint Empty project as shown in Figure 1. There select Fully Trust (Deploy to GAC) when prompt.

Then Add new SharePoint Event Receiver as shown in Figure 2 & 3.


And select the list to add event receiver. Here I’ll select Custom List (Figure 4).

Then open ItemEventReceiver.cs to write whatever you want when something happen, like Add new item, Update item and Delete item from the list. You can find commented methods for insert your code. I’ll show how you can get the ID of newly added item (Figure 5).

Deploying the Feature

Using Visual Studio 2008 extensions for Windows SharePoint you can easily deploy your feature to the SharePoint server. To do this you have to be in the server and you can deploy by right clicking on the project (Figure 6).

Then activate the "ProjectUpdateEventItemEventReceiver" at Site Settings --> Site Features.  

To me, it did not work when I tried to change the code and redeploy it. I had to use different name when I want to change the code.

After Deploying the feature you can check the result using EventReceiver-Installer for SharePoint.

You may notice new feature is activated for all the custom lists as above picture, now you can use same EventReceiver-Installer to delete unwanted events in other custom lists.

7 comments:

  1. Dude, You Rock.

    This is so much easier than all the things you have to do manually (.dll, GAC, Feature.XML) and then debugging issues were so time consuming.

    This process works wonders and saves developers a lot of time.

    Can you throw some light into the scope of this; I mean what does Scope ="Web" means.

    It means every list in the site or just for that site collection? Secondly, if we save site as a template then will the eventhandler work for the new instance of the site or not?

    Please write back if you have time?

    Many thanks,

    Prakash JJ

    ReplyDelete
  2. Hi Prakash JJ,

    Thanks for commenting. You can use the deployed feature in any site collection.

    When you deploying the feature you cannot specify to which site collection should the feature work. For example if you deployed a new feature for custom list, that event handler will work for each and every custom list in all the site collections.

    That’s why I mentioned about that tool called EventReceiver-Installer. Using that tool you have to remove the added event from other custom lists.

    I didn’t check for the site templates, but if it is not working you can simply activate the feature in Site Settings > Site Features.

    Thanks !

    ReplyDelete
  3. Is it not common to connect to a standard database connection and utilize stored procedures/tables etc? Do most MOSS web parts all need to uses lists to retrieve, update, delete records and so forth? I am converting some ASP.NET applications to web parts and really want to know the best way do this. I can already render a user control in the web part, but what about connections to the database and so forth. Is this not a common thing?

    -Chad

    ReplyDelete
  4. @ Data Springs,
    SharePoint lists are not familiar with basic database concepts. You have to use lists to store data and can use CAML queries to retrieve, update, delete records in SharePoint.

    If you want create web parts with visual design, please follow my article Create custom web part.

    First of all follow these articles to understand how to work with lists.
    thanks !

    ReplyDelete
  5. its really very gud and simple article to understand and implement

    ReplyDelete
  6. I have this error
    The file or assembly 'Microsoft.SharePoint.Library, Version = 15.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c' or one of its dependencies could not be loaded. The system could not find the file specified.

    ReplyDelete