I have never really had too much cause to use this method before but I am developing some generic classes to deal with column, content type and list management for use in a series of features.

My ListHelper class has a method called DeleteList which looks like this:

            Public Shared Function DeleteList(ByVal List As SPList, ByVal Operation As DeleteOperation) As Boolean

            If Operation = DeleteOperation.DeleteWithBackup Or Operation = DeleteOperation.ForcedDeleteWithBackup Then
                SaveListAsTemplate(List, True)
            End If

            If Operation = DeleteOperation.ForcedDelete Or Operation = DeleteOperation.ForcedDeleteWithBackup Then
                If List.AllowDeletion = False Then
                    List.AllowDeletion = True
                    List.Update()
                End If
            End If

            Try
                List.Delete()
            Catch ex As Exception
                Return False
            End Try

            Return True

        End Function

This is very much a work in progress, but I am having trouble with the SaveListAsTemplate method which looks like this:

            Public Shared Function SaveListAsTemplate(ByVal List As SPList, ByVal SaveContent As Boolean) As String

            If HttpContext.Current IsNot Nothing Then
                Dim tmpFileName As String = List.Title & "_saved_" & Now.ToFileTimeUtc & ".stp"
                List.SaveAsTemplate(tmpFileName, List.Title, "list template saved programmatically", SaveContent)
                Return List.ParentWeb.Site.Url & "/_catalogs/lt/" & tmpFileName
            Else
                Throw New Exception("List template cannot be saved unless the code is running in a valid HttpContext")
            End If

        End Function

The reason for the check into HttpContext is possibly not required, this is the reason for my post.  I cannot get this code to execute from within a console application.  Does the SPList.SaveAsTemplate method require a HttpContext?  If it does it is not metioned in any of the docs that I have seen.

As an aside any other comments on this code gratefully received.


EDIT: Exception and Stack Trace

**System.ArgumentException "Value does not fall within the expected range"** 
*at Microsoft.SharePoint.SPGlobal.HandleException(Exception ex)    at Microsoft.SharePoint.SPList.SaveAsTemplate(String strFileName, String strName, String strDescription, Boolean bSaveData)    at XXXXXX.SharePoint.Lists.ListHelper.SaveListAsTemplate(SPList List, Boolean SaveContent) in C:\SharePoint Development Projects\XXXXXX.SharePoint\XXXXXX.SharePoint\Lists\ListHelper.vb:line 145    at XXXXXX.SharePoint.Lists.ListHelper.DeleteList(SPList List, DeleteOperation Operation) in C:\SharePoint Development Projects\XXXXXX.SharePoint\XXXXXX.SharePoint\Lists\ListHelper.vb:line 121    at XXXXXX.SharePoint.Lists.ListHelper.DeleteList(SPWeb Web, String ListName, DeleteOperation Operation) in C:\SharePoint Development Projects\XXXXXX.SharePoint\XXXXXX.SharePoint\Lists\ListHelper.vb:line 111    at XXXXXX_Feature_Receiver.XXXXXXDataStructureFeatureFactory.CreateOrUpdateListInstances(SPWeb Web) in C:\TestBed\XXXXXX Feature Receiver Development\XXXXXX Feature Receiver\XXXXXXDataStructureFeatureFactory.vb:line 37    at XXXXXX_Feature_Receiver.XXXXXXDataStructureFeatureFactory.Initiate(SPWeb Web) in C:\TestBed\XXXXXX Feature Receiver Development\XXXXXX Feature Receiver\XXXXXXDataStructureFeatureFactory.vb:line 14    at XXXXXX_Feature_Receiver.Module1.Main() in C:\TestBed\XXXXXX Feature Receiver Development\XXXXXX Feature Receiver\Module1.vb:line 11    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()"*