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

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

Rate This
  • Comments 12

Requirement:

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.

itemupdating1

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:

feature.xml:

<?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 "

                     Scope="Web"

                     xmlns="http://schemas.microsoft.com/sharepoint/">

          <ElementManifests>

                   <ElementManifest Location="elements.xml" />

          </ElementManifests>

</Feature>

elements.xml:

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

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

          <Receivers ListTemplateId="101">

                   <Receiver>

                             <Name>ValidateOnSave</Name>

                             <Type>ItemUpdating</Type>

                             <SequenceNumber>10001</SequenceNumber>

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

                             <Class>ValidateOnSave.ValidateOnSave</Class>

                   </Receiver>

          </Receivers>

</Elements>

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

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

<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="6DDC0D86-F71B-4164-970B-E56EC3146901">

          <FeatureManifests>

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

          </FeatureManifests>

          <Assemblies>

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

                   </Assembly>

          </Assemblies>

</Solution>

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

;** ValidateOnSave.wsp **

.OPTION EXPLICIT

.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

itemupdating2

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.

itemupdating3

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

Leave a Comment
  • Please add 5 and 8 and type the answer here:
  • Post
  • PingBack from http://msdnrss.thecoderblogs.com/2008/02/28/

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

  • 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.

  • 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.

    Thanks,

    Ankit

  • hi

    do u know if this can be implemented as webpart?

  • 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

  • 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.

  • Hi Pranab,

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

  • Vishal,

    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 -.

  • 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();

    if(str1=="Final")

    it will thrown and null exception error.

    please suggest some way to update column value

  • 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:

    eg:

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

    Any suggestions would be greatly appreciated.

    Regards

    Gary

  • 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.

Page 1 of 1 (12 items)