DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to add a SPFeatureReceiver to a Custom ListInstance?

Posted by PANVEGA on January 19, 2009

In reference to my previous post about binding a Custom ContentType to a new ListInstance, I wanna solve this issues belowe with a SPFeatureReceiver class in Visual Studio.

First off the default view of your list does not contain those custom columns. When you select the item you will of course see all of your columns but they aren’t listed in the default view. Secondly the default content type for the list is still available to the user. In the case of a generic list the “Item” content type is still present. So when a user enters a new item they are able to select Item or your custom content type. This may not be desirable. Also if you are using the API to insert a new item into the list it will use the list’s default content type instead of the one you may be expecting. Once again this may not be desirable.

To overcome these restrictions we’re going to create a generic feature receiver that can be reused on any implementation using this technique.

First thing we’re going to do is to make some changes to our feature to both support a feature receiver and to pass in some properties (sse belowe). We’re going to pass in the list name and the content type name to the feature receiver code so that we do not have to hard code any values. This will be important for reuse.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”; Id=”46062026-A0D2-43ae-960D-49B1E5645D7D”
Title=”Banner Feature”
Description=”The feature contains a Custom Content Type for the custom Banner List.
In addition, the feature adds the JavaScript Code to a Delegate Control and creates a new Banner Webpart in a top MasterPage PlaceHolder.”
ReceiverAssembly=”CustomListReceiver, Version=, Culture=neutral, PublicKeyToken=9f4da00116c38ec5″

<ElementManifest Location=”ControlJS.xml” />
<ElementManifest Location=”ControlMP.xml” />
<ElementManifest Location=”columns.xml” />
<ElementManifest Location=”ctypes.xml” />
<ElementManifest Location=”ListInstance.xml” />
<Property Key=”DefaultContentType” Value=”BannerType”/>
<Property Key=”ListName” Value=”BannerList”/>


In addition, you have to add ReceiverAssembly  and ReceiverClass to the Feature Tag after building your VS project (see above).

First of all, you have to install the VSTO extension.

First open Visual Studio. Create a new empty Template –> Add a Class chosse EventReceiver from the list. Give it a name. The Microsoft.SharePoint namespace is already referenced through the template and the string name file, too.

When Visual Studio creates the list definition project, it generates two Microsoft Visual C# source files: ListEventReceiver.cs and ItemEventReceiver.cs. These files contain the implementations of classes that are derived from the SPListEventReceiver and SPItemEventReceiver classes, with overrides of the list and item event-handling methods.

Here we only need the ItemEventReceiver Class. You see your class inherits from the SPItemEventReceiver class. Change the Classe from SPItemEventReceiver to SPFeatureReceiver and import the abstract methods.

public class ListReceiverListEventReceiver : SPFeatureReceiver

public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.OpenWeb();

using (web)
string listName = properties.Feature.Properties[“ListName”].Value;
string defaultContentTypeName = properties.Feature.Properties[“DefaultContentType”].Value;
SPList list = web.Lists[listName];
SetDefaultContentType(list, defaultContentTypeName);
AddColumnsToDefaultView(list, defaultContentTypeName, web);


private void SetDefaultContentType(SPList list, string defaultContentTypeName)
SPContentType defaultContentType = list.ContentTypes[defaultContentTypeName];
list.ContentTypesEnabled = true;

SPFolder folder = list.RootFolder;
SPContentType[] orderedContentTypes = new SPContentType[1];
orderedContentTypes[0] = defaultContentType;

folder.UniqueContentTypeOrder = orderedContentTypes;


private void AddColumnsToDefaultView(SPList list, string defaultContentTypeName, SPWeb web)
SPContentType contentType = web.ContentTypes[defaultContentTypeName];
SPView defaultView = list.DefaultView;

foreach (SPField field in contentType.Fields)



public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { }

public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }

public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }


  • SetDefaultContentType method changes the DefaultView to the new created CustomContentType
  • AddColumnsToDefaultView adds the fileds from the CCT to your DefaultView

With this new feature receiver at your disposal you should be able to quickly build out custom lists, take advantage of content types and site columns, and provide a good user experience by defaulting your content type and adding your new fields to the default view of the list.

Different ways how  to add a dll to your SP Solution and deploy it on your server. Read one of my previous post.

More Information:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: