Find the folder for the list you are looking for in the Features directory and open the ListTemplates folder under that. If you were looking for the Announcements list type, you'd navigate to C: Open the XML file in here to find the following code. Pick out the Template ID Type value. Code Listing 1 Announcements. First you'll need to create the code which will run whenever you catch your desired event. Then you'll need to create a Feature to activate your event handler. Since event handlers are part of the Object Model, the first thing you'll need to do is to add a reference to the Microsoft.
Right click on the References folder and select Add. In the Add Reference dialog,. NET tab, scroll to the bottom and select the Microsoft. Next you'll want to rename Class1. You're now ready to begin writing your code. This will give access to the methods listedin the previous table.
You are trying to prevent an item from being deleted so you will be overriding the ItemDeleting method as seen in Figure 2. Figure 2 In the ItemDeleting method, you'll cancel the deletion before it happens.
Your code should look like Listing 2. Code Listing 2 using System; using System. Deploying an assembly to the GAC requires a strongly named assembly, so in the Project Properties dialog, click the Signing tab and check the box to sign the assembly as in Figure 3. Be sure to create a new key file if you haven't already. Normally this GAC deployment would be automated with post build steps or batch files, but has been explained in manual detail here as a learning exercise. Create a Feature to Enable the Event Handler The next task is to create and deploy a Feature that activates your event handler.
Call the first XML file Feature. Next, navigate to an existing feature in the C: You need to create a new folder for your event handler. Create a new folder with the same name as your event handler as in Figure 5. Navigate into the AnnouncementsList folder and open the Feature.
Copy the contents of the Announcements Feature. Now it's time to update your Feature. Next you want to give your Feature a title and description to match its purpose. Finally, set the scope of the feature to Web. Remove any additional elements such as Version and Hidden. When you're finished, your Feature. The ListTemplateID indicates which template type se table 5 this event handler targets, in your case the Announcements list type is The name of the receiver is the name of the project.
The type of receiver is the function you've overridden in your DLL. The SequenceNumber is a unique number that identifies this Feature. Microsoft has reserved a significant number of these sequence numbers, but any above 20, should be available. Copy the public key from the properties dialog as illustrated by Figure 6.
Finally, the class element contains the Namespace. Figure 6 The Elements. To use stsadm, open a command prompt window and navigate to the bin directory under the 12 folder. First you need to install the feature to the site collection. Use the following command: Once you've installed the feature, the final step is to activate this feature.
This task can be done from inside the SharePoint UI, but for your purposes stsadm. You should have a command window that looks like the one in Figure 7. Figure 7 Now go to your site and create an Announcements list and put a few items in it.
I've done so with my Sample Announcements list. Select an item and try to delete it. You should get an error message that reads "Announcements cannot be deleted from this list" see Figures 8 and 9. Many of these objects implement the IDisposable interface and should be disposed of after they've been used to prevent these objects from remaining in memory.
Scott Harris and Mike Ammerlaan from Microsoft have written an excellent article outlining these best practices recommendations titled Best Practices: Any developer interested in writing code targeting the SharePoint platform should read this article.
Scott and Mike illustrate many great practices and they warn you about the times that you shouldn't automatically dispose of every disposable object. NET developer community for many years. Matt spends dozens of hours after work on local and national community activities such as the SharePoint 1, 2, 3! Net Rocks and the ASP.