Search This Blog

2011-02-24

Create a List event receiver feature

Step 1-Create a class library project "EventReceiverFeature" and add reference the dll as follows-


Step 2-Create a custom list "EventReceiverList" and a Column "Company" (Single line of text)

Step 3-Feature.xml will be as follows-
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="82540374-276A-458b-BB6C-B605D35E8DD6"
Title="List Event Receiver Feature"
Description="This is my Event Receiver feature"
Hidden="false"
Scope="Web"
ImageUrl="menuprofile.gif"
ReceiverClass="EventReceiverFeature.FeatureReceiver"
ReceiverAssembly="EventReceiverFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d"
>
</Feature>

Step 4-"FeatureReceiver.cs" will be as follows-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace EventReceiverFeature
{
class FeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{


// access SPSite object for current site collection
// SPSite siteCollection = (SPSite)properties.Feature.Parent;
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList lstVendors = web.Lists["EventReceiverList"];
// add event handlers to vendors list
string asmName = "EventReceiverFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d";
string itemReceiverName = "EventReceiverFeature.ListItemEventReceiver";

// add event receiver to fire before existing item is deleted
lstVendors.EventReceivers.Add(SPEventReceiverType.ItemAdded,
asmName,
itemReceiverName);

// add event receiver to fire before existing item is deleted
lstVendors.EventReceivers.Add(SPEventReceiverType.ItemUpdated,
asmName,
itemReceiverName);


}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
string asmName = "EventReceiverFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d";

using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
SPDocumentLibrary proposals = (SPDocumentLibrary)web.Lists["EventReceiverList"];
SPEventReceiverDefinitionCollection erdc = proposals.EventReceivers;
SPEventReceiverDefinition erd = null;
bool found = false;

for (int i = 0; i < erdc.Count; ++i)
{
erd = erdc[i];
if (erd.Assembly == asmName && ((erd.Type == SPEventReceiverType.ItemAdded)||(erd.Type==SPEventReceiverType.ItemUpdated)))
{
found = true;
break;
}
} if (found)
erd.Delete();
}


}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
// throw new NotImplementedException();
}
}
}

Step 5-"ListItemEventReceiver.cs" will be as foll0ws-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace EventReceiverFeature
{
public class ListItemEventReceiver:SPItemEventReceiver
{
private string FormatCompanyName(string value)
{
return value.ToUpper();
}
public override void ItemAdded(SPItemEventProperties properties)
{
UpperFormat(properties);
}
public override void ItemUpdated(SPItemEventProperties properties)
{
UpperFormat(properties);
}
public void UpperFormat(SPItemEventProperties properties)
{
DisableEventFiring();
//SPListItemCollection col=SPContext.Current.Web.Lists[properties.ListTitle].Items.Count
//(SPSite(properties.SiteId).OpenWeb(properties.lists.RelativeWebUrl))

//for (int i = 0; i < SPContext.Current.Web.Lists[properties.ListTitle].Items.Count; i++)
//{
// properties.ListItem[i] = FormatCompanyName(properties.ListItem[i+1].ToString());
// properties.ListItem.Update();
//}
string CompanyName = properties.ListItem["Company"].ToString();
properties.ListItem["Company"] = FormatCompanyName(CompanyName);
properties.ListItem.Update();
EnableEventFiring();

}
}
}

Step 6-Copy the dll to the GAC

Step 7-Copy the EventReceiverFeature to the feature folder and activate the feature-

Step 8-Now add New Item to your list in Any case(Upper/Lower) and click OK


Step 9-The output will be as follows-

1 comment:

Ava joseph said...

Really great and impressiv post. This information is very useful for topic. I always visited your blog site.document management software and document management systems.