Search code examples
ironpythonspotfire

Spotfire SaveAs using ironPython


I am trying to SaveAs a document as a library item on 'onPropertyChange' event using ironpython script. the script`s code attached to the property :

# Import namespaces
from Spotfire.Dxp.Application import DocumentSaveSettings
from Spotfire.Dxp.Framework.Library import *

# Set the folder path and file name
folderName = "/Spotfire Test Folder/Reports"
fileName = "Test File"

# Set up the LibraryMangager and ensure that we can 
# access the folder path specified
libraryManager = Document.GetService(LibraryManager)
success, libraryFolder = libraryManager.TryGetItem(folderName, LibraryItemType.Folder)

# Embed the data
Document.Data.SaveSettings.EmbedAllSourceData = 1

# Save the document back to the Library
Application.SaveAs(libraryFolder, fileName, LibraryItemMetadataSettings(), DocumentSaveSettings())

Unfortunately, I get the following error :

Invalid operation 'BeginAggregatedTransaction' to the command history when in state 'Executing'

  • Is there a problem with the script ? in case not , is there a way to save as a library item using a script or an api function (via javascript or ironpython)?

Solution

  • Apparently all ironpython scripts in spotfire run in transactions, and some api functions, such as 'SaveAs' are trying to invoke a second transaction, which causes the script to fail.

    therefore, the 'SaveAs' function call should run on the application thread, and thereby get outside the transaction.

    the final code which works :

    # Import namespaces
    from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread
    from Spotfire.Dxp.Application import DocumentSaveSettings
    from Spotfire.Dxp.Framework.Library import *
    
    # Declaring the function which will run async
    def g(app, folder, fileName, metaData, saveSettings):
       def f():
          app.SaveAs(folder, fileName, metaData, saveSettings)
       return f
    
    # Set the folder path and file name
    folderName = "/Spotfire Test Folder/Reports"
    fileName = "Test File"
    
    # Set up the LibraryMangager and ensure that we can 
    # access the folder path specified
    libraryManager = Document.GetService(LibraryManager)
    success, libraryFolder = libraryManager.TryGetItem(folderName, LibraryItemType.Folder)
    
    # Executing the function on the application thread, and Save the document back to the Library
    Application.GetService[ApplicationThread]().InvokeAsynchronously(g(Application, libraryFolder, fileName, LibraryItemMetadataSettings(), DocumentSaveSettings()))
    

    according to Tibco`s answer :

    "Hopefully when Spotfire 7.5 is released we will have a more permanent solution to these types of issues since we can then select not to run the code inside a transaction from the UI."