Search This Blog

Monday, June 1, 2009

Preventing Item Deletions

I wanted to take a moment to thank all the readers which have contributed to this blog, their comments have been very useful. In fact, this posting is related to a particular situation that many SharePoint Administrators or Site Administrators might encounter when using SharePoint Lists or Document Libraries. The particular situation is when you are using a List or a Document Library to provide data to other depending Lists, Document Libraries, Content Types, etc. It even applies to Site Columns which are a useful way to provide data within your portal.

In order to prevent Items to be deleted from a List or a Document Library, a few things must happen first:

  • An assembly must have been installed to the Global Assembly Cache ( GAC )
  • An Event Receiver must be attached to the List or Document Library, in particular to the ItemAdding Event

All these technology terms sound overwhelming, so let's stop for a moment and explain briefly what we are talking about here. In Windows SharePoint Services 3.0 and MOSS 2007, Lists and Document Libraries have a variety of events that are fired when an Item is processed; to mention a few of the most important ones:

  • ItemAdding
  • ItemUpdating
  • ItemDeleting

For a complete list go to: MSDN - Event Fundamentals

Making it simple, we can specify code that will be processed when any of these events happen. This code to executed must be deployed to the GAC ( Global Assembly Cache ), once it is there the next step is to communicate to SharePoint by indicating the following:

  • ClassName, PublicKeyToken, Culture, Version and safety
  • Assembly and ClassName
  • Event to Attach to be attached to
  • Add this information to the SharePoint List or Document Library

To illustrate this process you will find the following two projects at www.codeplex.com/illustris :

  • Event Receiver Assembly
  • Event Receiver Manager

These must be downloaded and extracted. Complete the following steps prevent the deletion of items on a desired SharePoint List or Document Library:

  • Deploy assembly ( Demo.ProcessItemDeletion.dll ) contained in the Process Item Deletion project to the GAC
  • Locate the bin/debug folder
  • Open the Command Line

Ensure you have access to the Visual Studio SDK Tools

  • Install by using the GACUTIL –I –f Demo.ProcessItemDeletion.dll
  • Execute the Attach / Remove Event Receiver to List or Document Library Utility indicate the following parameters:
    • SharePoint Site URL
    • SharePoint List or Document Library Name
    • ClassName, PublicKeyToken, Culture, Version and safety
    • Assembly and ClassName
  • Click the Add Button

Once all these steps are completed successfully, any user who is not a Site Administrator will not be able to delete an Item.


Understanding the code

You are probably wondering what is really happening behind the scenes, and this is the explanation:

When you install the Demo.ProcessItemDeletion.dll Assembly:

public override void ItemDeleting(SPItemEventProperties properties)

{

base.DisableEventFiring();

if (!properties.OpenWeb().CurrentUser.IsSiteAdmin)

{

properties.Cancel = true;

properties.ErrorMessage = "Items cannot be deleted from this list.";

}

this.EnableEventFiring();

}

This code does the following:

  • Disables the code to execute recursively
  • Verifies if a Site Administrator is currently logged in
  • Cancels the deletion from happenning
  • Displays the folowing error message: "Items cannot be deleted form this list."

When you click the Add Button the following code is executed:

SPSite oSite = new SPSite(txtSite.Text.ToString());

SPList oList = oSite.OpenWeb().Lists[txtList.Text.ToString()];

oList.EventReceivers.Add(SPEventReceiverType.ItemDeleting, txtAssemblyName.Text.ToString(), txtClassName.Text.ToString());

oSite.Dispose();

This code does the following:

Opens the URL specified in the TextBox

Opens the List or Document Library specified in the TextBox

Communicates to SharePoint and tells it to execute the code in the previously installed assembly when the ItemDeleting Event is fired


Where to go from here?

That really depends on you, mostly because you can expand this code and make it more functional. For instance, you could provide a more detailed explanation of why the user could not delete the item. Or maybe you want to apply the same idea to updating items as well ( which would require the development of code in the Demo.ProcessItemDeletion project for the ItemUpdating Event ).

The whole purpose of this posting is to ignite ideas that will help you customize your portal to better fit your needs.


No comments:

Post a Comment