locked
about Event Handlers RRS feed

  • Question

  • Hi Guys,,


    can any one tell me how to delete custom list using Event Handlers in sharepoint 2010?

    Thursday, July 26, 2012 12:32 PM

Answers

  • Why do you want to use an event handler for deleting a list? Using the UI you can navigate to the list settings and use the "Delete this list" function. If you wand to use an event handler, you have to write the necessary code:

    SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
    
    SPList listToDelete = web.Lists["Name of your List"];
    listToDelete.Delete();
    web.Update();

    You could use this code in any Event, working on a concrete site, e.g. using a Feature and paste this code into the FeatureActivated method, but honestly that is a really strange way.

    What you could also do is to write a small console application, that gets the web and deletes the list but honestly, you could delete a list more easily using the UI or PowerShell...

    Best regards!

    Peter

    • Marked as answer by Pengyu Zhao Friday, August 3, 2012 9:08 AM
    Thursday, July 26, 2012 12:48 PM
  • in feature receiver you can use below code to delete the list

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    		{			
    			SPWeb web = properties.Feature.Parent as SPWeb;
    			SPList list = web.Lists.TryGetList("Your list name");
    			if (list != null)
    			{				
    				list.Delete();
    				web.Update();		
    			}
    		}


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by Pengyu Zhao Friday, August 3, 2012 9:09 AM
    Thursday, July 26, 2012 1:25 PM

All replies

  • Why do you want to use an event handler for deleting a list? Using the UI you can navigate to the list settings and use the "Delete this list" function. If you wand to use an event handler, you have to write the necessary code:

    SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
    
    SPList listToDelete = web.Lists["Name of your List"];
    listToDelete.Delete();
    web.Update();

    You could use this code in any Event, working on a concrete site, e.g. using a Feature and paste this code into the FeatureActivated method, but honestly that is a really strange way.

    What you could also do is to write a small console application, that gets the web and deletes the list but honestly, you could delete a list more easily using the UI or PowerShell...

    Best regards!

    Peter

    • Marked as answer by Pengyu Zhao Friday, August 3, 2012 9:08 AM
    Thursday, July 26, 2012 12:48 PM
  • in feature receiver you can use below code to delete the list

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    		{			
    			SPWeb web = properties.Feature.Parent as SPWeb;
    			SPList list = web.Lists.TryGetList("Your list name");
    			if (list != null)
    			{				
    				list.Delete();
    				web.Update();		
    			}
    		}


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by Pengyu Zhao Friday, August 3, 2012 9:09 AM
    Thursday, July 26, 2012 1:25 PM