Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - Development Tips on SharePoint, Office and Web

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search

News

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

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.

Posted: Thursday, February 28, 2008 9:37 PM by pranab

Comments

SHAREPOINTBlogs.com Mirror said:

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

# February 28, 2008 6:28 PM

Aaron Powell said:

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.

# March 1, 2008 4:37 AM

Ankit Patel said:

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

# September 30, 2008 3:31 AM

mossdev said:

hi

do u know if this can be implemented as webpart?

# January 6, 2009 7:25 PM

VIkash said:

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

# January 19, 2009 8:12 AM

Imir said:

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.

# February 9, 2009 5:04 AM

Vishal nagpal said:

Hi Pranab,

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

# May 29, 2009 7:43 AM

pranab said:

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

# May 29, 2009 7:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker