Workspace running through Dynamo API

This post is actually two questions related to the same class.

I have been trying to develop a mini Dynamo Sandbox to get the workspace from a filepath through the code below.

  • If Dynamo is closed, in order to read workspace information, I need to first make an empty JournalFiles call to dynamoRevit.ExecuteCommand(dynamoRevitCommandData) or the code below does not read information.

  • After calling the previous, despite reading workspace information, it does not run the script via;
    HomeWorkspaceModel homeWorkspace = workspacint as HomeWorkspaceModel;
    homeWorkspace.Run();

    using System;
    using System.Collections.Generic;
    using System.IO;
    using TaskDialog = Autodesk.Revit.UI.TaskDialog;
    using Newtonsoft.Json;
    using Dynamo.Core;
    using Dynamo.Graph.Workspaces;
    using Dynamo.Models;
    using Dynamo.Scheduler;
    using Dynamo.Engine;
    using Dynamo.Graph.Nodes.NodeLoaders;
    using Dynamo.Graph.Nodes;
    using Dynamo.Utilities;
    using System.Linq;
    using System.Reflection;
    using String = System.String;

    namespace NodeLibrary
    {
    public class NodesInfo
    {
    public static bool IsTestMode { get; set; }
    public static LibraryServices libraryServices { get; set; }

        public static EngineController engineController { get; set; }
        public static DynamoScheduler scheduler { get; private set; }
        public static NodeFactory nodeFactory { get; set; }
        public static CustomNodeManager customNodeManager { get; set; }
        
        public class DynamoPreferencesData
        {
            public double ScaleFactor { get; internal set; }
            public bool HasRunWithoutCrash { get; internal set; }
            public bool IsVisibleInDynamoLibrary { get; internal set; }
            public string Version { get; internal set; }
            public string RunType { get; internal set; }
            public string RunPeriod { get; internal set; }
    
            public DynamoPreferencesData(
              double scaleFactor,
              bool hasRunWithoutCrash,
              bool isVisibleInDynamoLibrary,
              string version,
              string runType,
              string runPeriod)
            {
                ScaleFactor = scaleFactor;
                HasRunWithoutCrash = hasRunWithoutCrash;
                IsVisibleInDynamoLibrary = isVisibleInDynamoLibrary;
                Version = version;
                RunType = runType;
                RunPeriod = runPeriod;
            }
    
            public static DynamoPreferencesData Default()
            {
                return new DynamoPreferencesData(
                  1.0,
                  true,
                  true,
                  AssemblyHelper.GetDynamoVersion().ToString(),
                  "Automatic",
                  RunSettings.DefaultRunPeriod.ToString());
            }
        }
        public static bool isValidJson(string path, out string fileContents, out Exception ex)
        {
            fileContents = "";
            try
            {
                fileContents = File.ReadAllText(path);
                fileContents = fileContents.Trim();
                if ((fileContents.StartsWith("{") && fileContents.EndsWith("}")) || //For object
                    (fileContents.StartsWith("[") && fileContents.EndsWith("]"))) //For array
                {
                    var obj = Newtonsoft.Json.Linq.JToken.Parse(fileContents);
                    ex = null;
                    return true;
                }
                ex = new JsonReaderException();
                return false;
            }
            catch (Exception e) //some other exception
            {
                Console.WriteLine(e.ToString());
                ex = e;
                return false;
            }
        }
    
        public static bool OpenJsonFile(
          string filePath,
          string fileContents,
          DynamoPreferencesData dynamoPreferences,
          bool forceManualExecutionMode,
          out WorkspaceModel workspace)
        {
            workspace = WorkspaceModel.FromJson(
                fileContents,
                libraryServices,
                engineController,
                scheduler,
                nodeFactory,
                IsTestMode,
                false,
                customNodeManager);
    
            workspace.FileName = filePath;
    
            HomeWorkspaceModel homeWorkspace = workspace as HomeWorkspaceModel;
            if (homeWorkspace != null)
            {
                homeWorkspace.HasRunWithoutCrash = dynamoPreferences.HasRunWithoutCrash;
    
                RunType runType;
                if (!homeWorkspace.HasRunWithoutCrash || !Enum.TryParse(dynamoPreferences.RunType, false, out runType) || forceManualExecutionMode)
                    runType = RunType.Manual;
                int runPeriod;
                if (!Int32.TryParse(dynamoPreferences.RunPeriod, out runPeriod))
                    runPeriod = RunSettings.DefaultRunPeriod;
                homeWorkspace.RunSettings = new RunSettings(runType, runPeriod);
    
                
            }
    
            CustomNodeWorkspaceModel customNodeWorkspace = workspace as CustomNodeWorkspaceModel;
            if (customNodeWorkspace != null)
                customNodeWorkspace.IsVisibleInDynamoLibrary = dynamoPreferences.IsVisibleInDynamoLibrary;
            
            return true;
        }
    
    
        public static string validjson;
    
        public static void RequestRun(string filepath)
        {
    
            Exception ex;
            
            NodesInfo.isValidJson(filepath, out validjson, out ex);
    
            NodesInfo.DynamoPreferencesData nuevo = NodesInfo.DynamoPreferencesData.Default();
    
            NodesInfo.OpenJsonFile(filepath, validjson, nuevo, true, out WorkspaceModel workspacint);
    
            HomeWorkspaceModel homeWorkspace = workspacint as HomeWorkspaceModel;
    
            homeWorkspace.Run();
    

    }