Skip to main content

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.

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.

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

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

Comments

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...