Thursday, September 22, 2011

Event Handler to prevent deletion of document from document library

  1. Start Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.



  3. In Project Types, under Visual Basic or C#, select Event Receiver.
  4. Type <Name for your project > as the project name. Click OK.
  5. In the SharePoint Customization Wizard, choose Deploy as a farm solution. Click Next.
  6. In the Choose Event Receiver Settings dialog, select List Item Events in the What type of event receiver do you want? choose from drop down dropdown.

  7. In the What item should be the event source? choose from dropdown, choose Tasks.
  8. Choose the An item is being deleted option in the Handle the following events list. Click Finish.
  9. In the EventReceiver1 file that is created, insert the following code in the ItemDeleting method.




using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace EventHandler
{
    class AnnouncementDeleteRevert: SPItemEventReceiver
    {
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            string errorMessageToDisplay= ("Announcements can not be deleted from this list");
            properties.ErrorMessage = HandledMessage;
            properties.Cancel = true;


//or


properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
        }
    }
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...