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.

Comments (12)

  1. Troy says:

    Hi there,

    I have to build a event hander that saves the URL to a list after the user clicks on the hyperlink. Its going to be used to track click thros. What would be the easiest way to accomplish this? Im fairly new to c# so would appreciate any help.

  2. Gary says:

    Hi, thanks for the interesting tutorial.

    Do you know how I can create a concatenated string string derived from the Sharepoint library metadata whenever a document is saved or edited?

    The purpose of this is to enforce a file naming convention whenever a document is saved to or edited in a Sharepoint Document Library:

    For example:

    Project ID + “ – “

    + Document Type + “(“

    + Tag + “)“

    + From + “ “

    + File Ref + “ “

    + “V” + Version

    The string for the record in the document library (document name)would read:


    0999 – Supplier Quotation (Furniture) Steve Smith QJD615 V3.0

    Any suggestions would be greatly appreciated.



  3. Navin says:

    Hi Pranab,

    i am facing one problem, i want to update one custom column value based on status column value e.g. if status = final the custom column value update.

    for this i used itemupdating event and in this i used string str1= properties.afterproperties["Status"].tostring();


    it will thrown and null exception error.

    please suggest some way to update column value

  4. Pranab-MSFT says:


    Go to the List settings page of List/Doc Lib and copy the ID from the URL. Change %7B and %7D to { and } and %2D to -.

  5. Vishal nagpal says:

    Hi Pranab,

    Can you please tell me from where i will get the value of strListID == "241fd82c-d67b-44aa-abb2-409df8c9942e

  6. Imir says:

    I tried to follow your steps but in the end I do not see any folder called Package and ValidateOnSave.wsp. Any help with this?

    thanks a lot.

  7. VIkash says:

    when u r uddating item u won’t be able to see the Drop Down selected value(The changed one) it will take the value which was present there , so kindly show me how to do that

  8. mossdev says:


    do u know if this can be implemented as webpart?

  9. Ankit Patel says:

    Hi Pranab,

    I am working in cimcon software as an module leader in .Net and Sharepoint area.

    I want to know that how can we give multiple receivers in one Elements.xml ?

    I hopw for your kind co-operation.



  10. Aaron Powell says:

    Just a tip, you’d be better off putting a FeatureReceiver on your feature so you can attach the EventHandler to the actual lists EventReceiver collection, rather than doing the ‘if’ statement. This removes overhead of hitting the event handler every time. Also if your site has multiple EventHandlers within your site you’ll really decrease the overhead.

  11. Requirement: I have a Doc Lib where I have some Metadata fields. There is one field called “My Choice