private ProcessState WaitForProcessFinish(ImportExportServiceClient client, string processId) { do { Thread.Sleep(1000); ProcessState?processState = client.GetProcessState(processId); if (processState == ProcessState.Finished || processState == ProcessState.Aborted || processState == ProcessState.AbortedByUser) { return(processState.Value); } }while (true); }
public string GetExportConfig(ExportConfigRequest request) { string output = String.Empty; // Check that outputFileWithPath yields a valid, existing folder. string folder = Path.GetDirectoryName(request.outputFileWithPath); if (!Directory.Exists(folder) || !request.outputFileWithPath.EndsWith(".zip")) { output = "Please ensure the Output Package is a valid full path with filename ending in \".zip\". Please ensure the specified folder is an existing folder."; return(output); } if (String.IsNullOrEmpty(request.input)) { output = "No valid items have been found to export."; return(output); } // Do this only after checking above if the request.input string is null or empty string[] tcms = request.input.Split(','); ImportExportServiceClient importExportClient = null; try { // Set up the app data for storing the endpoint addresses first, to ensure they get persisted. SetAppDataAddress("exportEndpointAddr", request.importExportEndpointAddress); SetAppDataAddress("streamDownloadAddr", request.streamDownloadAddress); // Retrieve credentials. string username = GetUserName(); // Password comes in as a hex string from the request. string encryptedPWAsHexString = request.encryptedPasswordAsHexString; byte[] encryptedPWAsByteArray = ConvertHexStringToByteArray(encryptedPWAsHexString); // Decrypt and strip pkcs#1.5 padding. var bytesPlainTextDataTest = csp.Decrypt(encryptedPWAsByteArray, false); // Get our original plainText back. // Note: bytes are apparently representing a UTF8 version of the decrypted text. var password = System.Text.Encoding.UTF8.GetString(bytesPlainTextDataTest); var credentials = new NetworkCredential(username, password); // These settings are taken from Tridion.ContentManager.ImportExport.Common.dll. Normally, these would be entered in a web.config file, // or similar, and pull in here simply by referencing the binding/endpoint in that config. But since there is no such file for this // Alchemy plugin, we opt set these up here. var endpointAddress = new EndpointAddress(request.importExportEndpointAddress); var binding = new BasicHttpBinding(); binding.Name = "ImportExport_basicHttpBinding"; binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; importExportClient = new ImportExportServiceClient(binding, endpointAddress); importExportClient.ChannelFactory.Credentials.Windows.ClientCredential = credentials; // TODO: Work on these settings // e.g. June Test Comp 2016 goes in package twice for some reason, currently (even though it's not localized, etc.): var exportInstruction = new ExportInstruction() { LogLevel = LogLevel.Normal, BluePrintMode = BluePrintMode.ExportSharedItemsFromOwningPublication, ExpandDependenciesOfTypes = {}, ErrorHandlingMode = ErrorHandlingMode.Skip }; var selection = new Selection[] { new ItemsSelection(tcms) }; var processId = importExportClient.StartExport(selection, exportInstruction); var processState = WaitForProcessFinish(importExportClient, processId); if (processState == ProcessState.Finished) { // Use the same credentials as the above client // TODO: validate outputFileWithPath DownloadPackage(processId, request.outputFileWithPath, credentials, request.streamDownloadAddress); } importExportClient.Close(); output += "Success"; } catch (Exception e) { importExportClient.Abort(); output += e.ToString() + System.Environment.NewLine + e.StackTrace; } return(output); }