Developing and Deploying WSS 3.0 Events

Introduction

Windows SharePoint Services 2.0 was very limited when we talked about the event handlers.  Event hander will capture various things happening on sites, lists, document libraries, like creating a site, deleting a site, adding list item, deleting list item, document checked out and checked in etc.  WSS 2.0 only support Events for Document libraries.  No support was available for lists and sites.  WSS 3.0 now adds much of the functionality into the events.  WSS 2.0 includes asynchronous events only which means that they were fired after something is happened and there is no way of stopping it.  WSS 3.0 now includes asynchronous event but also provide synchronous events as well.  Events like ItemDeleting, SiteDeleting, ListColumnAdding etc will provide a great infrastructure for developer to create great application on Windows SharePoint Services 3.0. 

Creating Event Handlers

Creating event hander is also very easy.  Events can be create by using Visual Studio.NET 2005 class library and inheriting from WSS receiver class and overriding the methods to handle WSS events.  In this scenario I will demonstrate a small logging application that logs what is currently going on inside a document library. 

Technologies Used

1.       .NET Framework 2.0

2.       Visual Studio.NET 2005

3.       SQL  Server 2000

4.       C#.NET

5.       Web Parts

 

Getting Ready for the Session

Description

Screenshot

Ø  Create a New WSS Web Site

Ø  Click Start

Ø  Microsoft Office Server

Ø  SharePoint 3.0 Central Administration

Ø  On Central Administration Page Click on Application Management

Ø  Under SharePoint Site Management Click Create a Site Collection

Ø  On create site collection page

Ø  Select the web application where you wanted to create a Site Collection using web application dropdown

Ø  Enter the required name in the Title Text box

Ø  Type description if required

Ø  Select the Sites Directory in the URL path and enter the site URL name

Ø  Select Blank site

Ø  Click OK

Ø  Enter primary and secondary site collection administrator and Click OK

Ø  Wait for the progress page

Ø  When you see site collection creation success page.  Click on the link to visit the site.

Ø  Blank site will look like this

 

Ø  On the site collection page click on the Site Actions and Create

Ø  On the create page under the libraries section click Document Library

 

Ø  Enter Document Library name (Secret Docs in this case)

Ø  Type description

Ø  Select Yes to Show in Quick Launch

Ø  No for Create Version on each edit

Ø  Select Microsoft Office Word if you have Office 2003 Select Office Word 97/2000 format

Ø  Click OK

Create a Custom List

 

Ø  On the site collection page click on the Site Actions and Create

Ø  On the create page under the Custom Lists section click Custom List

Ø  Enter List Name as CEO Docs Access Logs

Ø  Type Description

Ø  Yes to Show on Quick Launch

Ø  Click Create

Ø  Add a new column to custom list and name it as Event Date

 

Ø  Adding Both Library and List as web parts on the home page

 

Ø  On Site Actions

Ø  Click Edit Page

Ø  Page will look like this

Ø  Click on Add a Web Part

Ø  On Add web part to left page

Ø  Select both document library and Lists

Ø  Click Add

 

Ø  Click on the CEO Document Library Header Drag it to on top of List to move it above the list.

Ø  Click Exit Mode

Ø  Page will Look like this

Developing Event Handler

Description

Screenshot

Ø  Start Visual Studio.NET 2005

 

Ø  Create a C# Class Library Project

Ø  Add a Reference to Microsoft.SharePoint.dll

Ø  In Visual Studio Click Project

Ø  Add Reference

Ø  Scroll Down to see Windows SharePoint Services.dll File

Ø  Select the File and Click OK or Double Click on the file

Ø  Import the Microsoft SharePoint by using “using” keyword

Ø  Rename the class file to CEODocEventReceiver.cs

Ø  The code segment will look like this

Ø  Inherits this class from SPListItemEventReceiver Class in SharePoint

Ø  Now we need to test methods of SPListItemEventReceiver class.  Lets override them. 

Ø  Go to the class definition and add

o   ItemAdding

o   ItemAdded

o   ItemUpdating

o   ItemUpdated

Ø  You can see that SharePoint now support both types of event handlers synchronous and asynchronous. 

Ø  Your class will look like this.

Ø  Lets add code to item adding

Ø  Create a Method to populate the Log list create above

Ø  This method will receive the message from the various events of the lsits

private void AddLogEntry(string strMessage)

        {

            //Collection Site collection using Spsite

            SPSite siteCollection = new SPSite("http://jerry/sites/WSSEvents");

            //Collection the web

            SPWeb webSite = siteCollection.RootWeb;

 

            //Get the log lists

            SPList ItemList = webSite.Lists["CEO Docs Access Log"];

            //add a new time

            SPListItem Item = ItemList.Items.Add();

            // Set properits

            Item["Title"] = strMessage;

            Item["Event Date"] = DateTime.Now.TimeOfDay.ToString();

            Item.Update();

            webSite.Dispose();

        }

Ø  Lets write the code in ItemAdding Event to log the creation of an item to the list

public override void ItemAdded(SPItemEventProperties properties)

        {

            // Collect the SPlistitem from the EventProperties

            SPListItem anItem = properties.ListItem;

            // Create a String to write an entry

            string strMessage = "An Item is Added: " + anItem.Title.ToString() + ":" + anItem["Name"].ToString();

            // Call the Log method create above.

            AddLogEntry(strMessage);

        }

Ø  Now add the code to others events

For this example I am have used the same code but changed the string text.

Ø  Sign the Assembly

Ø  Goto solution properties in Visual Studio 2005

Ø  Click Signing and Check Sign the assembly checkbox

Ø  Select New from the drop down and enter a name

Ø  Uncheck password protected and click OK

Ø  Build the DLL and put the solution DLL into Global Assembly Cache

Registering a Event Handler

There are two way to register the event handlers

1.       SP Object Model

2.       Using Features

1.       Registering events using SharePoint Object Model

In this method we use the SharePoint object model to register the event.  We collection the SharePoint site and Web and get the list where we want to bind the events. 

2.       Registering events using Features

Features are package of Windows SharePoint Services elements to do some sort of action like installing any application or program at the root level and enabling its activation or deactivation on root site or any other site we want. 

Registering Event using SharePoint Object Model

1.       Things required to register the event

2.       SPList Object

3.       SPWebObject

4.       SPListObject

5.       AssemblyName

6.       Class Name

1.       Create a new Console Based Application and name it as you want

2.       Every List has a property EventReceivers

3.       EventReceivers object Requires

a.       EventType = Enumeration

b.      Assembly Name

c.       Class Name

4.       Go to main of the console application and add the following code

SPSite siteCollection = new SPSite("http://jerry/sites/WSSEvents");

            //Collection the web

SPWeb webSite = siteCollection.RootWeb;

 

SPList CEOSecretDocs = webSite.Lists["Secret Docs"];

 

string strAssemblyName = "CEOLibraryEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=de977b68a8e516e5";

string strClassName = "CEOLibraryEvents.CEOEvents";

 

CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemAdded, strAssemblyName, strClassName);

            CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemAdding, strAssemblyName, strClassName);

CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemUpdatedstrAssemblyName, strClassName);

            CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemUpdating, strAssemblyName, strClassName);

Run the Console Application to install the event handlers

Hit F5 in Visual Studio.

Test the Events

To Test the event go to the site

1.       Click New Item on CEO Docs Document library

2.       Save the Item

3.       A task will be added to the list

4.       Now Open the item again and Edit it and save it again

5.       Task for Updating and Updated will be added to the log list

 

Events are key to transforming Microsoft Windows SharePoint Services into a true developer platform. Events enable first and third parties to hook into Windows SharePoint Services behaviors and override default behaviors.

Events fall into two major categories:

·         List events   Core events, including changes, additions, and removals of list items and list columns (schema changes)

·         Simple site events   Deletion of sites and site collections

Events are either synchronous "before" events, denoted by the "XYZing" name format, or asynchronous “after” events, denoted by the "ABCed" name format.

·         Event receivers can be registered, through the "Features" feature, with an Item, a List, a Web, or a Content Type. For example, an event receiver that ensures a document always has a copyright in the footer can be associated with a central Content Type and pushed down to all document libraries associated with that type.

·         As they do in Microsoft Windows SharePoint Services (version 2), developers can override and extend the event handler method. A new sequence number parameter allows for control of the firing order of multiple events.

I have used SharePoint Object Model to deploy the Event handlers.  In the next example I will show how to install them using Features.  So stay tuned.

Regards,

JERRY

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s