Create and deploy Event Receiver Feature and Solution package

Here is a Step-by-Step description of how to create an Item event receiver as a feature and then creating a solution package for it. I this code we will be creating an event receiver for document library types templates where we will assign a value to a custom field called "Location" on adding of a document.

Remember - document added is ItemAdded and if we need to assign any value to document properties ( like Title, File Type etc.) we need to use ItemUpdating.

So lets start.

1. Create a new asp.net Webapplication project and name it as "myEventReceiver".

2. Add Refernces to Microsoft.Sahrepoint.dll in the Project.

3. Create the below two folders in your project

a. ItemEventreceiver

b. Package

4. Add the below three files in the ItemEventreceiver folder (created in step 3)

a. Add a xml file and name it as elements.xml
b. Add a xml file and name it as feature.xml
c. Add a class file and name it as EventReceive.cs

5. Now add another xml file in the project (not in the ItemEventreceiver folder) and name it as manifest.xml.

6. Lastly, add the below two text type files in your project

a. mysolution.ddf
b. mypackage.bat

7. Sign the Project. Generate a key pair using sn.exe tool and save it to your local machine. Now go to Project Properties and in signing tab browse for this key.
Build the Project and get the full name of the dll or assembly from the reflector.

Now lets add some code in these files

EventReceive.cs -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;


namespace EventReceivernamespace
{
public class EventReceiver : SPItemEventReceiver
{

public override void ItemUpdated(SPItemEventProperties properties)
{

SPItem oItem = properties.ListItem;

oItem["Location"] = "New Jersey";

this.DisableEventFiriing();

oItem.SystemUpdate(false);

this.EnableEventFiring();

}
}
}


elements.xml


<?xml version="1.0" encoding="utf-8" ?>
<Elements Id="sfb3c25c-f311-4dd3-86de-29904210c7d5" xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>ItemUpdated</Name>
<Type>ItemUpdated</Type>
<SequenceNumber>2000</SequenceNumber>
<Assembly>myEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9275960caf741af9</Assembly>
<Class>EventReceivernamespace.EventReceiver</Class>
</Receiver>
</Receivers>
</Elements>


Use the fully qualified name of the assembly for this project in your assembly tag.

fetaure.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="2e4d1d48-589e-4328-8a24-a554b2cdcbad"
Title="mycustomImageReceiverFeature"
Scope="Web"
Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>



manifest.xml


<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="d7f69035-e74c-44f2-aaa8-928418f6ead0" xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureManifests>
<FeatureManifest Location="ItemEventreceiver\feature.xml" />
</FeatureManifests>
<Assemblies>
<Assembly Location="myEventReceiver.dll" DeploymentTarget="GlobalAssemblyCache">
<SafeControls>
<SafeControl Assembly ="myEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9275960caf741af9"
Namespace ="EventReceivernamespace" TypeName ="*" />
</SafeControls>
</Assembly >
</Assemblies>
</Solution>


mysolution.ddf -


.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=mycustomEventReceiver.wsp -> .wsp name
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=Package

;adds manifest file
manifest.xml

;adds webpart dll
bin\myEventReceiver.dll


;sets the title webpart feature directory
.Set DestinationDir=ItemEventreceiver

;adds the feature manifest to the feature directory
ItemEventreceiver\feature.xml

;adds the element manifest to the feature directory
ItemEventreceiver\elements.xml


Optional
mypackage.bat - This file will create the soltuion package for you and will place it in the Package folder ( that you created in step 3) on build of your project. In addition to that this will also drop the .wsp file in 12 hive's BIN folder (Make sure you have Permissions to drop the .wsp in BIn directory).


@SET MakeCabTool=makecab.exe
@SET WSP=mycustomEventReceiver
@SET DESTDIR="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
@SET SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe

if EXIST Package\%WSP%.wsp del Package\%WSP%.wsp
"%MakeCabTool%" /f mysolution.ddf /D CabinetNameTemplate=Package\%WSP%.wsp

if EXIST Package\%WSP%.cab del Package\%WSP%.cab
"%MakeCabTool%" /f mysolution.ddf /D CabinetNameTemplate=Package\%WSP%.cab

XCOPY /r /y Package\*.wsp %DESTDIR%

Lastly, Edit the Project Properties and under Build Events in "Post Build event command line" box write the below

CD $(ProjectDir)
mypackage.bat

Now build the Project. You should see the mycustomEventReceiver.wsp in your package folder and in SharePoint's BIN directory as well

Now add your solution in sharepoint solution store via stsadm and deploy it.

To do this,

Open the stsadm.exe (from 12 hive bin) and type the below command

stsadm -o addsolution -filename mycustomEventReceiver.wsp

Now got to Cental admin -> Operations-> solution management and click on mycustomEventReceiver.wsp. Click on Deploy and choose one of the web applications that you want to deploy the feature on.

Lastly, Browse to that web application -< Site Settings -< Site features and activate the feature.

To Test -

1. Create a custom document library and create a column "Location" in it.
2. Now upload the document.
3. Once your done uploading and adding properties like Title and stuff for the document you will see the Location column populated with the value "New Jersey"

Yeh!

3 comments:

  1. For SP2010:
    http://msdn.microsoft.com/en-us/library/gg252010.aspx

    ReplyDelete
  2. Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
    Sharepoint Training in Chennai

    ReplyDelete
  3. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com