Working with ListAdding and ListDeleting Events

  • December 4, 2009
  • By Razi bin Rais
  • More Articles »
SharePoint 2010 now provides events for the Lists, an expected and welcome update. There are a number of events that are added to the list event, but this article will take a look at two of them as shown in blue in the chart, namely ListAdding, ListDeleting. The subsequent article will show and discuss “ListAdded” and “ListDeleted” events, shown in grey.

What these events bring is power to fulfill complex business requirements. For example consider a scenario where you want to delay the deletion of specific lists and only allow immediate list deletion if deletion is initiated by certain user, or you may want to stop the addition of a new List based on type of the list.

To sum up, it not only opens doors for managing complex requirements it also provides room to be more innovative when approaching business scenarios. In this example, you will see how to limit the creation of lists based on List Template ID, i.e. on the basis of the type of list, plus you will also see how the list deletion works.

The example uses Visual Studio 2010 Beta 2, along with SharePoint Server 2010 Beta. Once you have the development environment setup you can proceed as follows.

1. Launch the Visual Studio 2010, and create a new project. Expand “Visual C#” (you can also choose Visual Basic, but this example will use Visual C#). Select “SharePoint” from the “Installed Template” and then select “2010”; from the right-hand pane select “Event Recevier”. In the “Name” field type “ListEvents” and in the “Solution Name” type in “Tip-12-ListEvents”.

NOTE: Make sure that from the top drop down “.NET Framework 3.5” is selected. Also make sure that your project is targeted against “x64” not “x86”, you can change it once project is created, not from New Project dialog.

Finally click “OK” to create the project.

2. Select “Deploy as a farm Solution” and click next.

3. Select “List Events” and then check events as shown below.

4. The following figure shows the newly created Project and its files.

5. The stubs of the events that you selected earlier are already created; what is required is to start putting in code for desired behavior.

6. For the first scenario, the “ItemAdding” event will be used, and as this event fires up before the List is actually created, the List object cannot be used, as it will be null at this moment. Instead “properties” object in the method parameter (of type “SPListEventProperties”), has “TemplateId” property which contains the Template ID of the List, that is about to be created.

Also note that it also contains a property called “ListTitle” which gives title of the list. Now, to stop creation of any new Document Library, check the “TemplateId” such that it’s not equal to “101” which refers to Document Library. Following code fragment shows this.

7. For the second scenario, you will write code inside “ListDeleting” method, the check is made to inquire if the list has items inside it, in which case deletion is cancelled. See the following code.

8. Click “F5” inside the Visual Studio to “Build” and “Deploy” the solution. Visual Studio will take care of deploying the ListEvents handlers.

9. On the SharePoint Site, click on “Site Actions,” “View All Site Content”.

10. Click “Create.”

11. Click on the “Document Library”, and type “Sample” in the text box, and then click “Create” button.

You will see an error message from “ListAdding” event handler and as a result Document Library will not be created.

12. Now for the second scenario, you need to have a list (any SharePoint list) with some items in it, for this you may want to create a custom list and then add some elements to it (not shown here), and then finally try to delete it. You will see the following error from the “ListDeleting” Event and List will not be deleted.

To sum up, the new Lists events provide great benefit to developers to fulfill the requirements both form IT admins sites and from business users. In the subsequent article the “ListAdded” and “ListDeleted” events will be discussed along with new option of showing users custom error page while cancelling the events.

1


Networking Solutions





Partners