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.
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="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>
elements.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="https://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="https://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
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
Anonymous
February 28, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/02/28/Anonymous
February 28, 2008
Requirement: I have a Doc Lib where I have some Metadata fields. There is one field called “My ChoiceAnonymous
February 29, 2008
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.Anonymous
September 29, 2008
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, AnkitAnonymous
January 06, 2009
hi do u know if this can be implemented as webpart?Anonymous
January 19, 2009
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 thatAnonymous
February 08, 2009
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.Anonymous
May 28, 2009
Hi Pranab, Can you please tell me from where i will get the value of strListID == "241fd82c-d67b-44aa-abb2-409df8c9942eAnonymous
May 29, 2009
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 -.Anonymous
December 10, 2009
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 valueAnonymous
September 05, 2010
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
- Anonymous
May 16, 2011
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.