SharePoint 2007 (MOSS/WSS) – Creating an ItemUpdating Event Handler as a feature and Packaging it in .wsp


I have a Doc Lib where I have some Metadata fields. There is one field called “My Choice” which is a Choice type field with 3 values – “Choice1”, “Choice2” and “Choice3”. By Default the selection is “Choice2”. Now there is another field called “My Value” of Text Type. My requirement is, if user selects Choice1 in the My Choice field, then the My Value field cannot remain blank. This is not possible OOB, but can be done using a custom ItemUpdating event handler.


To start with create a Class Library project in VS 2005. Add reference of Windows SharePoint Services namespace. I named it as ValidateOnSave. Here is the code:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace ValidateOnSave


    public class ValidateOnSave : SPItemEventReceiver


        public override void ItemUpdating(SPItemEventProperties properties)


            SPWeb site = properties.OpenWeb();

            Guid ListID = properties.ListId;

            string strListID = ListID.ToString();

  if (strListID == "241fd82c-d67b-44aa-abb2-409df8c9942e")


                int ListItemID = properties.ListItemId;

                SPListItem newListItem = site.Lists[ListID].GetItemById(ListItemID);

                string myChoice = properties.AfterProperties["My Choice"].ToString();

                string myValue = properties.AfterProperties["My Value"].ToString();

                if (myChoice == "Choice1" && myValue == "")


                    properties.Cancel = true;

                    properties.ErrorMessage = "Please insert a value in My Value field";






Now create a folder within the VS project called ValidateOnSave. Add following 2 XML files there:


<?xml version="1.0" encoding="utf-8" ?>

<Feature  Id="3BA74762-9CE3-4de3-A172-A4054FF9D2E3"

                   Title="Validate user input on save"

                   Description="This feature is used to validate user input on save "




                   <ElementManifest Location="elements.xml" />




<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="">

          <Receivers ListTemplateId="101">





                             <Assembly>ValidateOnSave, Version=, Culture=neutral, PublickeyToken=e3ad17290add8aac</Assembly>





Add another xml file called manifest.xml file at the root of the project:

<?xml version="1.0" encoding="utf-8" ?>

<Solution xmlns="" SolutionId="6DDC0D86-F71B-4164-970B-E56EC3146901">


                   <FeatureManifest Location="ValidateOnSave\feature.xml"/>



                   <Assembly DeploymentTarget="GlobalAssemblyCache" Location="ValidateOnSave.dll">




Add a text file at the root of the project and rename it as cab.ddf. Here is the content:

;** ValidateOnSave.wsp **


.Set CabinetNameTemplate=ValidateOnSave.wsp

.Set DiskDirectoryTemplate=CDROM

.Set CompressionType=MSZIP

.Set UniqueFiles="ON"

.Set Cabinet=on

.Set DiskDirectory1=Package


manifest.xml manifest.xml

ValidateOnSave\elements.xml ValidateOnSave\elements.xml

ValidateOnSave\feature.xml ValidateOnSave\feature.xml

bin\debug\ValidateOnSave.dll ValidateOnSave.dll

Add another text file at the root of the project and rename it as installer.bat. Here is the content:

makecab /f cab.ddf

Here is the screenshot of the Solution Explorer


Now sign the assembly and build the solution.

Now you need a Windows Utility called MakeCab.exe. If you haven’t it added it in system path, then open the project in Windows explorer and copy the MakeCab.exe to the root of the project.


Now double-click on the installer.bat and you will find a folder called Package and ValidateOnSave.wsp under it.