1. Create a new SharePoint Project.
Project types: SharePoint
Template: Empty
Give a Project Name. For my case,
Project Name: Helpdesk_EventHandler
2. Add the necessary reference required for the project.
Since it is an event handler for SharePoint, it is mandatory to include the following Reference:
Microsoft.SharePoint
Reference is available in
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll
3. Create a Feature folder and ItemEventReceiver subfolder in the project according to how it will appear in the Feature folder under 12-Hives.
4. Create a feature.xml that looks like the following in the ItemEventReceiver subfolder. <Feature
Id ="69942DC7-8AB2-4204-9E3C-8CFB7BEADC37"
Title ="EventHandler"
Description ="This feature is used to restrict users from creating an Item in the List with same code value. Please do not de-activate this feature."
Scope="Web"
Hidden="FALSE"
AlwaysForceInstall ="TRUE"
ImageUrl =""
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location ="ItemEventReceiver.xml"/>
</ElementManifests>
</Feature>
Id – Goto Tools > Create GUID to create a new unique ID for the feature.
Title – This is the name it will appear on the Site Settings > Site Features.
Description – This is the brief description below the name appearing on the Site Settings > Site Features.
Scope – Farm|WebApplication|Site|Web
ElementManifests – You need to specify the ElementManifest file. This is very IMPORTANT. For more reference on feature.xml file, refer to the following:
http://msdn.microsoft.com/en-us/library/ms475601.aspx
http://msdn.microsoft.com/en-us/library/ms436075.aspx
5. Create the ItemEventReceiver.xml that looks like the following in the ItemEventReceiver subfolder. <Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
<Receivers ListTemplateId ="100">
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemAdding</Type>
<SequenceNumber>1000</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>1001</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
</Receivers>
<Receivers ListTemplateId ="106">
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemAdding</Type>
<SequenceNumber>1000</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>1001</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
</Receivers>
<Receivers ListTemplateId ="1001">
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemAdding</Type>
<SequenceNumber>1000</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
<Receiver>
<Name>ItemEventReceiver</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>1001</SequenceNumber>
<Assembly>EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a</Assembly>
<Class>EventHandler.ItemEventReceiver</Class>
<Data></Data>
</Receiver>
</Receivers>
</Elements> Important Notes:
Use unique sequence number for different types of list differentiated by ListID (ListTemplateID).
To find out the custom ListTemplateID you are going to attach the event handler to. Know the Custom List you use. Navigate to All Site Content, click Create. Mouse over the Custom List you use to create the list and read the ListTemplate value of the url string shown in the status panel. (This is the number you going to use as the ListTemplateId.)
Look at the Class tag ‘EventHandler.ItemEventReceiver’.
Namespace: EventHandler
Public Class: ItemEventHandler
Type: Refers to <Calling Events>.
Assemble is a 4-part assemble information consisting of: o Assembly Name
o Assembly Version
o Culture (set it to neutral to accept the default language culture information)
o PublicKeyToken (The ID generated and representing the assembly upon compilation. You can check out the PublicKeyToken using Reflector.NET.)
Reflector.NET can be obtained from http://www.red-gate.com/products/reflector/.
For more information and reference on how to create this xml for Event Handling in Sharepoint, refers to Brian Wilson’s blog again at http://blogs.msdn.com/brianwilson/archive/2007/03/05/part-1-event-handlers-everything-you-need-to-know-about-microsoft-office-sharepoint-portal-server-moss-event-handlers.aspx.
Alternatively, you can copy these two xml files and modify according to your need (following the two steps [4, 5] listed.)
6. Before we proceed, jus ensure your assembly is SIGNED. If your assembly is signed, it should have a snk file. If NOT, signed the assembly by RIGHT CLICK on the project. Sign the assembly under the Signing section by choosing a Strong name key file without protecting the key file with a password.
7. Try compiling the project and use Reflector to get the PublicKeyToken value.
8. Create a manifest.xml file as shown. <?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="{E5084205-6D14-4fec-93FB-6E63CD8E9856}" xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureManifests>
<FeatureManifest Location="ItemEventReceiver\Feature.xml"/>
</FeatureManifests>
<Assemblies>
<Assembly DeploymentTarget="GlobalAssemblyCache" Location="EventHandler.dll">
<SafeControls>
<SafeControl
Assembly="EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=512983bb7f57c77a"
Namespace="EventHandler"
TypeName="*" Safe="True" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution> Important Notes:
SolutionId – Goto Tools > Create GUID to create a new unique ID for the feature.
Include a FeatureManifests section to specify location of the feature.xml required by the feature.
Include the Assemblies section. Ensure that the feature created in type safe by specifying it within the SafeControls element tag within the Assemblies section.
You may copy this sample manifest.xml and amend accordingly.
9. Create the code to enforce no duplicates in a SharePoint list during item creation or update by forcing the field as the primary key. In this case, I’m using the Title column of the List created to check for no duplicates. First, create an EMPTY class.
10. Sample code as shown: using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace EventHandler
{
public class ItemEventReceiver : SPItemEventReceiver
{
const string TITLE_QUERY = @"<Where><Eq><FieldRef Name=’Title’ /><Value Type=’Text’>{0}</Value></Eq></Where>";
/* Item Deleted Synchronous Event */
public override void ItemAdding(SPItemEventProperties properties)
{
//properties.Cancel = true;
//properties.ErrorMessage = "You cannot delete the Item from the document library. Please contact Administrator to de-activate this feature from this site.";
if (properties.AfterProperties["Title"] != null)
{
//get the title of the new item
string currentTitleCode = properties.AfterProperties["Title"].ToString();
//get the web site object
using (SPWeb web = properties.OpenWeb())
{
//get the current list
SPList list = web.Lists[properties.ListId];
//query the list to check if there are items with the same title
SPQuery q = new SPQuery();
q.Query = string.Format(TITLE_QUERY, currentTitleCode);
SPListItemCollection itemsWithSameCode = list.GetItems(q);
//if there are items, cancel the add, and show an error to the user.
if (itemsWithSameCode.Count > 0)
{
properties.Cancel = true;
properties.ErrorMessage = "There is already an item with the Code \"" + currentTitleCode + "\"" + " in this list.";
}
}
}
}
/* Item Deleted Synchronous Event */
public override void ItemUpdating(SPItemEventProperties properties)
{
//properties.Cancel = true;
//properties.ErrorMessage = "You cannot delete the Item from the document library. Please contact Administrator to de-activate this feature from this site.";
if (properties.AfterProperties["Title"] != null)
{
//get the title of the new item
string currentTitleCode = properties.AfterProperties["Title"].ToString();
//get the web site object
using (SPWeb web = properties.OpenWeb())
{
//get the current list
SPList list = web.Lists[properties.ListId];
//query the list to check if there are items with the same title
SPQuery q = new SPQuery();
q.Query = string.Format(TITLE_QUERY, currentTitleCode);
SPListItemCollection itemsWithSameCode = list.GetItems(q);
//if there are items, cancel the add, and show an error to the user.
if (itemsWithSameCode.Count > 1)
{
properties.Cancel = true;
properties.ErrorMessage = "There is already an item with the Code \"" + currentTitleCode + "\"" + " in this list.";
}
}
}
}
}
}
Important Note:
CAML will be covered separately in probably my next few post(s). So I will not talk about CAML here. 11. Once DONE, Save Files.
12. Build your Solution.
13. Install the feature solution.
stsadm -o addsolution -filename {WSPFILENAME}
14. Deploy the feature solution.
stsadm -o deploysolution -name {WSPFILENAME} -url {SITEURL}
15. Activate the feature (from the site in SharePoint where the feature was deployed to).
Alternative command-line activation:
stsadm -o activatefeature -id {FEATUREID} -url {SITEURL} -force
No comments:
Post a Comment