public JsonResult Deploy() { try { // read the json from the body of the request var jsonBody = new StreamReader(Request.InputStream).ReadToEnd(); // parse the json var templates = JsonConvert.DeserializeObject <List <JsonSitecoreTemplate> >(jsonBody); // import the templates var manager = new SitecoreDataManager(); var success = manager.ImportTemplates(templates); var response = new JsonResponse() { Success = success }; return(new JsonResult() { Data = response }); } catch (Exception ex) { Log.Error("SitecoreUML Deploy Exception: An error occurred while deploying templates to Sitecore", ex, this); return(new JsonResult() { Data = new JsonResponse() { Success = false, ErrorMessage = ex.Message } }); } }
public override void Execute(CommandContext context) { var inputFolderPath = SitecoreUMLConfiguration.ImportDropFolder; var inputDirectory = new DirectoryInfo(inputFolderPath); // if the import file drop path doesn't exist then terminate and show an error if (!inputDirectory.Exists) { Log.Error($"SitecoreUML Import Error: Drop path could not be found at the configured location: {inputFolderPath}", this); Sitecore.Context.ClientPage.ClientResponse.Alert($"SitecoreUML Import Error: Import Drop Folder was not found: {inputFolderPath}"); return; } var inputFiles = inputDirectory.EnumerateFiles("*.json").ToArray(); if (!inputFiles.Any()) { Log.Error("SitecoreUML Import Error: Input files could not be found at the configured location. See the configuration for more.", this); Sitecore.Context.ClientPage.ClientResponse.Alert("SitecoreUML Import Error: Import files were not found in the drop folder. See configuration for more."); return; } // deserialize the json var importDataSets = GetImportDataSets(inputFiles); // if data sets are null or empty report to the log and the client, and then terminate if (importDataSets == null) { Log.Warn("SitecoreUML Import Error: No import data sets were found in the import files. Process terminated.", this); Sitecore.Context.ClientPage.ClientResponse.Alert("SitecoreUML Import Error: No import data sets were found in the import files. Process terminated."); return; } // get the root item to add the templates to var templateRoot = _database.GetItem(SitecoreUMLConfiguration.Instance.TemplatesRootPath); if (templateRoot == null) { Log.Error($"SitecoreUML Import Error: Template root path does not exist: '{SitecoreUMLConfiguration.Instance.TemplatesRootPath}'.", this); Sitecore.Context.ClientPage.ClientResponse.Alert($"SitecoreUML Import Error: Template root path does not exist: '{SitecoreUMLConfiguration.Instance.TemplatesRootPath}'."); return; } // execute the import var importManager = new SitecoreDataManager(); if (!importDataSets.All(set => importManager.ImportTemplates(set, templateRoot))) { // TODO: add support for rolling back the import and update the logging when complete Log.Error("SitecoreUML Import Error: An error occurred while importing the template sets. Process terminated early. Please review your content tree for artifact files.", this); Sitecore.Context.ClientPage.ClientResponse.Alert("SitecoreUML Import Error: An error occurred while importing the template sets. Process terminated early. Please review your content tree for artifact files."); return; } // delete or archive the import data // TODO: implement delete or archiving functionality // refresh the template root so the imported items show Sitecore.Context.ClientPage.SendMessage(this, $"item:refreshchildren(id={templateRoot.ID})"); // show the client a success message Sitecore.Context.ClientPage.ClientResponse.Alert("Import completed successfully!"); }