Windows SharePoint Services 3.0 Custom Events
Prompted by a requirement that entails customization of SharePoint list/library event? With the aid of Microsoft Visual Studio 2005 SharePoint Extensions, we are provided with facilities to override the default events of WSS 3.0 lists and libraries, and such could easily be implemented without writing copious code. This paper attempts to provide us implementation guidance.
For demonstration purposes, we will override the default ItemDeleting event of Windows SharePoint Services 3.0 Document Library by changing the default error message and cancelling deletion of item. Additionally, it is assumed that we have (a) downloaded and installed Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions, and (b) created a Document Library in our SharePoint Web application. To synchronize with our code, the Document Library should be named Custom Events.
1. We will start by opening our Microsoft Visual Studio 2005 and create a new SharePoint Empty Project. For our purpose, let us name it Extensions. Below is the illustration:
2. Subsequently, we will add an Event Receiver item to our project. To do this right-click the project in the Solution Explorer > Add > New Item. In the Add new Item dialog box, as illustrated below, select Event Receiver, name it CustomEvent, and then click Add. A Create Event Receiver dialog box will be displayed.
3. Since we will customize a Document Library, as illustrated below, select Document Library from the drop-down list in the Create Event Receiver dialog box, and then click Add. A CustomEvent folder with ItemEventReceiver.cs, ItemEventReceiver.xml, ListEventReceiver.cs, and ListEventReceiver.xml files will automatically be added to our project.
4. Now it is time for us to write few lines of code. Let us open the ItemEventReceiver.cs file. Uncomment the ItemDeleting event, and then write the code below:
public override void ItemDeleting(SPItemEventProperties properties)
if (properties.ListTitle == "Custom Events")
properties.ErrorMessage = "For demonstration purposes, deleting is not allowed.";
properties.Cancel = true;
Notice that the SPItemEventProperties ErrorMessage and Cancel will only be reversed if the name/title of a Document Library is Custom Events. Else, it will affect all Document Libraries in our Web application. Below is the illustration.
5. To automate the deployment process, let us set the Start Action of our project. To do this, click Project > Project Name Properties. In the Project Properties page, select Debug tab. And in the Start Action section, select Start Browser with URL option, and then write http://server name/. Below is the illustration.
6. Finally, let us build and deploy our project. To do this, click Build > Debug Project Name. If successful, click Build > Deploy Project Name. In one of my blogs, I have explained the items that Microsoft Visual Studio SharePoint Extensions are automatically executing during the deployment process.
7. Upon successful deployment, let us open our SharePoint Web application, navigate to our Custom Events Document Library, add a document, then attempt to delete such document, and you would expect retention of the document with an error message illustrated below.
Hoping this would help, I am.