4

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()"

6
  • what happens when you run it from a Console Application? Do you get an exception? If so, could you please post the exception, including the stacktrace? Commented Feb 3, 2010 at 13:40
  • It throws an InvalidArgumentException. The stack trace points to the global exception handler in the SPList.SaveAsTemplate method. Commented Feb 3, 2010 at 13:45
  • There is no exception message? Commented Feb 3, 2010 at 15:02
  • value does not fall within the expected range Commented Feb 3, 2010 at 15:44
  • using Reflector, I can see that SPList.SaveAsTemplate does do something with the HttpContext. Could you please post the FULL stack trace? Commented Feb 3, 2010 at 16:23

3 Answers 3

1

Do you have the proper permissions to perform a template save to the Site Template Gallery? The identity executing the code must be site collection administrator if it mimics the user experience.

2
  • Interesting point! Commented Feb 4, 2010 at 15:35
  • Yes the code is being run in a console app by a user with Full Control via Web App Policy. The user is also in the Farm admins group so pormissions is not likely to be the issue. Commented Feb 5, 2010 at 8:56
0

Not sure why it would behave differently in a Console App, but I would try saving the template with a very basic filename for the .stp

Remove the timestamp just for testing purposes, as this could contain characters that are invalid to SharePoint.

1
  • I have tried with "temp.stp" but to no avail. I am wondering if the issue is with the way in which I am getting a reference to the SPList object. Commented Feb 3, 2010 at 15:46
0

You will often get this error if the filename already exists at the location you are trying to save it. SaveListAsTemplate will not override an existing file. Additionally it could be an invalid character in the filename, I found this API is more restricting than the Windows FileSystem for filenames, it does not allow certain characters even though they are valid for the filesystem. In addition if it works fine within a web and not within the console try to add AllowUnsafeUpdates right before the SaveAsTemplate call since a Console app will not have a security context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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