Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have two list that I need to delete when a solution is deactivated and removed. But my code doesn't seems to be working. I am trying to delete and remove the quicklinks to the lists in FeatureDeactivating event receiver:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb webSite = properties.Feature.Parent as SPWeb)
            {

                if (webSite != null)
                {
                    SPList customerList = webSite.Lists.TryGetList("SandBoxProjet - Customers");
                    if (customerList != null)
                    {
                        customerList.OnQuickLaunch = false;

                        SPListItemCollection lic = customerList.Items;
                        foreach (SPListItem item in lic)
                        {
                            item.Delete();
                        }
                        customerList.Update();
                        customerList.Delete();
                    }

                    SPList leadList = webSite.Lists.TryGetList("Sales Lead");
                    if (leadList != null)
                    {
                        leadList.OnQuickLaunch = false;

                        SPListItemCollection lic = leadList.Items;
                        foreach (SPListItem item in lic)
                        {
                            item.Delete();
                        }
                        leadList.Update();
                        leadList.Delete();
                    }

                    webSite.Update();
                }
            }
        }

What am I doing wrong? I am deploying the solution as sandbox. The list seems to live even after the solution is deactivated and I am able to make modifications to the data.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

It’s better to use the Delete() method of SPWeb.Lists instead of using the Delete() method on the SPList because the latter doesn't delete lists properly sometimes. Try something like:

SPWeb mySite= SPContext.Current.Web;
SPList myCustomList = mySite.Lists["MyCustomList"];
mySite.Lists.Delete(myCustomList.ID);
mySite.Update();
share|improve this answer
    
your code seems to do the trick! but with a slight modification: SPWeb webSite = properties.Feature.Parent as SPWeb; instead of SPWeb mySite= SPContext.Current.Web;! Why? Any reason? –  Abdel Olakara Jun 17 '12 at 18:47
1  
SPContext.Current.Web fetches the current context from the w3wp.exe process. As the sandbox code does not run in the w3wp.exe process, it is not able to get the context. The sandbox code runs in the SPUCWorkerProcess.exe process. –  Vardhaman Deshpande Jun 17 '12 at 19:22
1  
code was just a sample to illustrate the usage of 'SPWeb.Lists.Delete'. Of course, SPContext works in the context of Sandbox solutions: msdn.microsoft.com/en-us/library/… –  Falak Mahmood Jun 17 '12 at 19:49
add comment

I just did a mini code review of your code. (I hope you don't mind). Please see if the following code works:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //No need to use the using block here.
            //Since you are using the SPWeb object provided by the properties object,
            //you do not need to dispose it explicitly. The object will be handled by
            //the SP Framework itself.
            SPWeb webSite = properties.Feature.Parent as SPWeb

                if (webSite != null)
                {
                    SPList customerList = webSite.Lists.TryGetList("SandBoxProjet - Customers");
                    if (customerList != null)
                    {
                        customerList.OnQuickLaunch = false;

                       //You do not need to delete every single item in the list individually.
                       //If you delete the list, each item in it will automatically get deleted.
                        customerList.Delete();
                    }

                    SPList leadList = webSite.Lists.TryGetList("Sales Lead");
                    if (leadList != null)
                    {
                        leadList.OnQuickLaunch = false;

                       //You do not need to delete every single item in the list individually.
                       //If you delete the list, each item in it will automatically get deleted.
                        leadList.Delete();
                    }

                    webSite.Update();                 
                }
        }
share|improve this answer
    
Thanks for reviewing the code. I agree my code is little aggresive by deleting every list item. But I already tried doing it your way and failed. Did the above code "work" for you? –  Abdel Olakara Jun 17 '12 at 18:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.