546 17.3 Working with Document Libraries—Event Exercise
There are plenty of tools accessible via the Internet and downloadable
for free, that allow you to easily register and unregister event handlers
through a GUI type application. Alternatively, you can create your own cus-
tom application now that you know how to enumerate, register, and unregis-
ter event handlers.
17.3 Working with Document Libraries—Event
Exercise
The WSS 3.0 event architecture has improved greatly since its predecessor
and provides developers with a great platform for extending the base prod-
uct functionality. In this final section, we will walk through an exercise that
demonstrates the creation of an event handler that overrides the
SPItemEventReceiver class.
Those of you familiar with managing the permissions and roles for pub-
lic folders in Microsoft Exchange should be aware that you can assign per-
missions to determine which users are allowed the ability to create folders.
SharePoint Server 2007 and WSS 3.0 gives you the option to turn off the
create folder interface, which basically determines whether the “New Folder”
option appears on the New menu. This menu option is available on the Doc-
ument Library Advanced Settings page (Figure 17.29). It does not remove
the functionality itself, nor does it apply any permissions-based logic for
accessing the Create Folder page. If you know how to navigate to the Create
Folder page, by perhaps remembering the URL, then you can create a folder
as you would if the menu option existed. Furthermore, out of the box you
cannot define which users are allowed the ability to create or delete folders
based on their group membership or permissions; however, this functionality
can be provided through events.
In this exercise we will create an event handler that ensures only mem-
bers of the “Classified Users” SharePoint group have the ability to create or
delete folders in all the document libraries within a site. Users who are not
members of the “Classified” SharePoint group may only delete the docu-
ments that they created. In addition, when a document is added to a docu-
ment library, a link to that document is inserted into the “Classified Links”
list at the root of the site collection. Upon deletion of a document, we also
need to remove any links to that document that have been added to the
“Classified Links” list. Please note that the code used throughout this chapter
is for demonstration purposes only, as it has not been thoroughly tested and
does not contain the appropriate exception handling required of a produc-
tion application. Nevertheless, it illustrates the power of and some potential
uses for leveraging the event architecture when extending and customizing
your SharePoint implementation.
17.3 Working with Document Libraries—Event Exercise 547
Chapter 17
At a very high level, the steps involved include creating an event han-
dler that inherits the
SPItemEventReceiver class and overrides the
ItemDeleting, ItemAdding, and ItemAdded events. The ItemDeleting
method checks the content type, then checks for group membership to
determine whether the user is allowed to proceed. The
ItemAdding
method also checks the content type of the item being added, then checks
for group membership to determine whether the user is allowed to create a
folder in the document library. Finally, the
ItemAdded event checks the
content type of the item that was just added to the document library, and if
it is a document, a link to that document is added to the “Classified Links”
list. Once the event handler is created, we will deploy the event handler and
register it using a Feature. Note that we are using C# for this exercise.
Figure 17.29
Tur n folders on
and off.
Get Microsoft SharePoint 2007 Technologies now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.