MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Friday, November 16, 2007

Delete List (forget about forcedeletelist)

Okay - so I'm trying to test some import issues with lists and decide to export an existing list that has documents set the way I need them. I run my import test and realize I've got more work to do so I go to delete the list via the browser. Hmm... the delete option isn't there - no problem I say - there's a forcedeletelist command that I just blogged about last week so I'll use that. But what's this - I ran the command and I got this error: "The requested delete operation could not be performed. (Exception from HRESULT:0x800720CE)".

Okay - what the heck! What's the point of a "FORCE"deletelist command if it doesn't FORCE the deletion of the list. So I did some testing and I discovered that the reason it couldn't delete it was because there was a property called "AllowDeletion" which was set to false (my list was the "Documents" list which is part of the publishing feature so it's set to not allow deletion). I checked this property out and it turns out that it's a really simple property with public getter and setters (the later being unusual in my experience - I usually have to use reflection to hack the objects). So I set the property to true and then tried a simple SPList.Delete() and it worked like a charm.

So what did I conclude - forcedeletelist is misnamed and should be abandoned - I've created a new command called simply gl-deletelist which allows you to pass a "-force" parameter in to actually force the deletion of the list. Now granted - there may be cases in which mine still fails but at least it will handle the AllowDeletion setting! Here's the code:

   1: internal static void Delete(SPList list, bool force, string url)
   2: {
   3:  if (list == null)
   4:   throw new SPException("List not found.");
   5:  
   6:  if (!list.AllowDeletion && force)
   7:  {
   8:   list.AllowDeletion = true;
   9:   list.Update();
  10:  }
  11:  else if (!list.AllowDeletion)
  12:   throw new SPException("List cannot be deleted.  Try using the '-force' parameter to force the delete.");
  13:  
  14:  try
  15:  {
  16:   list.Delete();
  17:  }
  18:  catch (Exception)
  19:  {
  20:   if (force)
  21:   {
  22:    using (SPSite site = new SPSite(url))
  23:    {
  24:     Utilities.RunStsAdmOperation(
  25:      string.Format(" -o forcedeletelist -url \"{0}\"",
  26:           site.MakeFullUrl(list.RootFolder.ServerRelativeUrl)), false);
  27:    }
  28:   }
  29:  }
  30: }
The syntax of the command can be seen below:
C:\>stsadm -help gl-deletelist

stsadm -o gl-deletelist

Deletes a list.

Parameters:
        -url <list view URL>
        [-force]
Here's an example of how to delete a list passing in the force parameter:
stsadm –o gl-deletelist -url "http://intranet/Documents/Forms/AllItems.aspx" -force

6 comments:

swastik nath said...

hi,
Great tool .
you saved my day.
keep up the good work.

regards,
swastik.

Anonymous said...

I'm having this problem and would like to use your solution, but I don't understand something. You provided the code to use to set the AllowDeletion to true, but where do I input and/or execute this code?

Gary Lapointe said...

Go to my downloads page (link at top of page) and download and install the WSP file. You can then use stsadm extension from there. The code posted is just for reference so people know what I'm doing with these extensions.

Avinash Kota said...

Thank You Gary, this helped me out. You STSADM extensions are cool. Nice job!

Avinash Kota
http://sharepointreporter.wordpress.com

Anonymous said...

I deleted the Document Library "Documents" from a Team Site (-force), now my navigation browse functionality is broken because it is looking for. Anything on fixing this issue?

Gary Lapointe said...

You could try adding the list back or otherwise reactivating the feature that creates it (I think it's the team collab one). Beyond that I'd need you to further explain what is wrong with the navigation as I'm not really following.