/// <summary> /// 连接到ServerApplication /// </summary> private MFilesServerApplication ConnectToServerApplication() { try { if (_serverApp == null) { _serverApp = new MFilesServerApplication(); } var authType = MFAuthType.MFAuthTypeSpecificMFilesUser; if (_adminUser.AccountType == MFLoginAccountType.MFLoginAccountTypeWindows) { authType = MFAuthType.MFAuthTypeSpecificWindowsUser; } _serverApp.ConnectAdministrative(null, authType, _adminUser.Name, _adminUser.Pwd, "", "ncacn_ip_tcp", _adminUser.ServerIp); } catch (Exception) { _serverApp = null; } return(_serverApp); }
/// <summary> /// Executes a search by display/external id using the API directly. /// </summary> static void UseApiDirectly() { // Connect to the server (localhost, tcp, current Windows user). var application = new MFilesServerApplication(); application.ConnectAdministrative(); // Get a connection to the vault. var vault = application.LogInToVault(Program.sampleVaultGuid.ToString("B")); // Create the basic search conditions collection. var searchConditions = new SearchConditions(); // Create the search condition. SearchCondition condition = new SearchCondition { ConditionType = MFConditionType.MFConditionTypeEqual, }; condition.Expression.DataStatusValueType = MFStatusType.MFStatusTypeExtID; condition.TypedValue.SetValue(MFDataType.MFDatatypeText, Program.customerDisplayId); // Add the condition to the collection. searchConditions.Add(-1, condition); // Search. var results = vault.ObjectSearchOperations.SearchForObjectsByConditions(searchConditions, MFSearchFlags.MFSearchFlagNone, SortResults: false); // Output the number of items matching (should be one in each object type, at a maximum). Console.WriteLine($"There were {results.Count} objects with the display Id of {Program.customerDisplayId}:"); Console.WriteLine($"Complete."); // Disconnect. vault.LogOutSilent(); application.Disconnect(); }
/// <summary> /// Installer Method - Install app to Vault. /// </summary> /// <param name="packedApp">mfappx Path</param> /// <param name="settings">Populated UXDevSettings Object</param> private static void InstallApp(string packedApp, UXDevSettings settings) { // Create a caption block for the installation. using (Logger.CaptionBlock("Connecting to Vault")) { // Connect to Vault var serverApp = new MFilesServerApplication(); // report the creation Logger.WriteLine("Server App Created => Connecting Administratively"); // Connect Administrative => As the Take Vault on/off-line requires it. serverApp.ConnectAdministrative( null, settings.AuthType, settings.Username, settings.Password, settings.Domain, settings.ProtocolSequence, settings.ServerAddress, settings.Port, settings.LocalComputerName ); // Report the successful connection. Logger.WriteLine(@"Connected to server."); // Log into the Vault Vault vault = serverApp.LogInToVault(settings.VaultGuid); // Report the successful Log On. Logger.WriteLine(@"Logged into Vault: " + vault.Name); // create a caption block to make the output more readable. using (Logger.CaptionBlock("Installing Application")) { // Try to install the application. try { Logger.WriteLine(@"Attempting Application Installation"); // Install the Application to the Vault. vault.CustomApplicationManagementOperations.InstallCustomApplication(packedApp); // If no error was thrown, report the success. Logger.WriteLine(@"Application Installed Successfully"); } catch (Exception ex) { // A common error is the already exists. Logger.WriteLine(ex.Message.StartsWith(@"Already exists") ? ex.Message.Substring(0, ex.Message.IndexOf('\n')) : ex.Message); } } // create a caption block to make the output more readable. using (Logger.CaptionBlock("Post Installation Processing")) { // Determine if a Auto-Allow Installation Registry file should be created. if (settings.GenerateAutoInstallReg) { Logger.WriteLine("Generating the Auto Installation Registry File"); // Get the Template Text and inject values string reg = StringTemplate.AllowSilentInstall; reg = reg.Replace("$VERSION", vault.GetServerVersionOfVault().Display); reg = reg.Replace("$NAME", Path.GetFileNameWithoutExtension(settings.Outputname)); reg = reg.Replace("$VAULTGUID", vault.GetGUID()); reg = reg.Replace("$APPGUID", settings.AppDef.guid); // create the registry file path. string regPath = Path.Combine(settings.DirectoryToZip, @"allow_auto_install.reg"); // Delete and destroy the existing reg file... if there is one. if (File.Exists(regPath)) { File.Delete(regPath); } // Write the reg file to disk. File.WriteAllText(regPath, reg); Logger.WriteLine("Registry File Created: " + regPath); } // Should the vault be restarted after processing? if (settings.RestartVault) { Logger.WriteLine("About to Restart the Vault"); Logger.WriteLine("\t- Logging out."); // TODO - This does not seem to work, a manual logout is recommended. vault.LogOutSilent(); // Try to take the vault on/off-line. try { Logger.WriteLine("\t- Taking Vault Offline"); serverApp.VaultManagementOperations.TakeVaultOffline(settings.VaultGuid, true); Logger.WriteLine("\t- Bringing Vault back Online"); serverApp.VaultManagementOperations.BringVaultOnline(settings.VaultGuid); Logger.WriteLine("Vault Now Online"); } catch (Exception ex) { // report the error Logger.WriteLine(ex.Message); } // TODO - I need to find a better way to close open client windows, but until then... if (settings.KillExplorerWindows) { Logger.WriteLine("Closing Open Explorer Windows."); foreach (Process p in Process.GetProcessesByName("explorer")) { p.Kill(); } } Logger.WriteLine(@"Waiting a few seconds..."); Thread.Sleep(1000 * 2); } // Determine if we should open the Vault Window for the Developer. if (Settings.OpenVault) { Logger.WriteLine(@"Opening Path: " + settings.LocalVaultPath); // Open the path in the default application. Process.Start(Settings.LocalVaultPath); } } } // Should we leave the console window open or not? if (!settings.AutoExitApp) { Logger.NewLine(); Logger.WriteLine(@"Press any key to exit..."); Console.ReadKey(); } }
/// <summary> /// Executes a segmented search using the API directly. /// </summary> static void UseApiDirectly() { // Connect to the server (localhost, tcp, current Windows user). var application = new MFilesServerApplication(); application.ConnectAdministrative(); // Get a connection to the vault. var vault = application.LogInToVault(Program.sampleVaultGuid.ToString("B")); // Load the object types from the vault. Console.WriteLine("Loading object types..."); var objectTypes = vault .ObjectTypeOperations .GetObjectTypes() .Cast <ObjType>() .ToList(); Console.WriteLine($"Iterating over {objectTypes.Count} object types..."); // Iterate over the object types to count the objects. foreach (var objectType in objectTypes) { // Create the basic search conditions collection. var searchConditions = new SearchConditions(); // Add a condition for the object type we're interested in. { // Create the search condition (for object type id). SearchCondition condition = new SearchCondition { ConditionType = MFConditionType.MFConditionTypeEqual }; condition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeObjectTypeID, null); condition.TypedValue.SetValue(MFDataType.MFDatatypeLookup, objectType.ID); // Add the condition at the index provided. searchConditions.Add(-1, condition); } // Create variables for the segment information. const int itemsPerSegment = 1000; // Maximum number of items in each segment. var segment = 0; // Start; this will increment as we go. var moreItems = true; // Whether there are more items to load. var countIncludingDeleted = 0; // The count of matching items. // Whilst there are items in the results, we need to loop. while (moreItems) { // Execute a search within the object id segment. { // Clone the search conditions (so we can add current-segment condition). var internalSearchConditions = searchConditions.Clone(); // Add search condition: // Id within the range: (segment - itemsPerSegment) to ((segment + 1) * itemsPerSegment) { // Create the search condition. SearchCondition condition = new SearchCondition { ConditionType = MFConditionType.MFConditionTypeEqual }; condition.Expression.SetObjectIDSegmentExpression(itemsPerSegment); condition.TypedValue.SetValue(MFDataType.MFDatatypeInteger, segment); // Add the condition at the index provided. internalSearchConditions.Add(-1, condition); } // Execute the search and increment the count. countIncludingDeleted += vault.ObjectSearchOperations .SearchForObjectsByConditionsEx(internalSearchConditions, MFSearchFlags.MFSearchFlagDisableRelevancyRanking, SortResults: false, MaxResultCount: 0, SearchTimeoutInSeconds: 0).Count; // Move to the next segment. segment++; } // Are there any more items? { // Clone the search conditions (so we can add object id condition). var internalSearchConditions = searchConditions.Clone(); // Add search condition: // Id at least (segment * itemsPerSegment) { // Create the search condition. SearchCondition condition = new SearchCondition { ConditionType = MFConditionType.MFConditionTypeGreaterThanOrEqual }; condition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeObjectID, null); condition.TypedValue.SetValue(MFDataType.MFDatatypeInteger, segment * itemsPerSegment); // Add the condition at the index provided. internalSearchConditions.Add(-1, condition); } // If we get one item then there's more results. moreItems = 1 == vault.ObjectSearchOperations.SearchForObjectsByConditionsEx( internalSearchConditions, // Our search conditions. MFSearchFlags.MFSearchFlagDisableRelevancyRanking, SortResults: false, // Don't bother attempting to sort them. MaxResultCount: 1, // We only need to know if there is at least one, nothing more. SearchTimeoutInSeconds: 0).Count; } } // Output the stats. Console.WriteLine($"\t{objectType.NamePlural}:"); Console.WriteLine($"\t\tTotal: {countIncludingDeleted} (included deleted)"); } Console.WriteLine($"Complete."); // Disconnect. vault.LogOutSilent(); application.Disconnect(); }
/// <summary> /// Installer Method - Install app to Vault. /// </summary> /// <param name="packedApp">mfappx Path</param> /// <param name="settings">Populated UXDevSettings Object</param> private static void InstallApp( string packedApp, UXDevSettings settings) { // Create a caption block for the installation. using( Logger.CaptionBlock( "Connecting to Vault" ) ) { // Connect to Vault var serverApp = new MFilesServerApplication(); // report the creation Logger.WriteLine("Server App Created => Connecting Administratively"); // Connect Administrative => As the Take Vault on/off-line requires it. serverApp.ConnectAdministrative( null, settings.AuthType, settings.Username, settings.Password, settings.Domain, settings.ProtocolSequence, settings.ServerAddress, settings.Port, settings.LocalComputerName ); // Report the successful connection. Logger.WriteLine(@"Connected to server."); // Log into the Vault Vault vault = serverApp.LogInToVault(settings.VaultGuid); // Report the successful Log On. Logger.WriteLine(@"Logged into Vault: " + vault.Name); // create a caption block to make the output more readable. using( Logger.CaptionBlock( "Installing Application" ) ) { // Try to install the application. try { Logger.WriteLine(@"Attempting Application Installation"); // Install the Application to the Vault. vault.CustomApplicationManagementOperations.InstallCustomApplication(packedApp); // If no error was thrown, report the success. Logger.WriteLine(@"Application Installed Successfully"); } catch (Exception ex) { // A common error is the already exists. Logger.WriteLine( ex.Message.StartsWith( @"Already exists" ) ? ex.Message.Substring( 0, ex.Message.IndexOf( '\n' ) ) : ex.Message ); } } // create a caption block to make the output more readable. using (Logger.CaptionBlock("Post Installation Processing")) { // Determine if a Auto-Allow Installation Registry file should be created. if (settings.GenerateAutoInstallReg) { Logger.WriteLine("Generating the Auto Installation Registry File"); // Get the Template Text and inject values string reg = StringTemplate.AllowSilentInstall; reg = reg.Replace("$VERSION", vault.GetServerVersionOfVault().Display); reg = reg.Replace("$NAME", Path.GetFileNameWithoutExtension(settings.Outputname)); reg = reg.Replace("$VAULTGUID", vault.GetGUID()); reg = reg.Replace("$APPGUID", settings.AppDef.guid); // create the registry file path. string regPath = Path.Combine(settings.DirectoryToZip, @"allow_auto_install.reg"); // Delete and destroy the existing reg file... if there is one. if (File.Exists(regPath)) File.Delete(regPath); // Write the reg file to disk. File.WriteAllText(regPath, reg); Logger.WriteLine("Registry File Created: " + regPath); } // Should the vault be restarted after processing? if (settings.RestartVault) { Logger.WriteLine("About to Restart the Vault"); Logger.WriteLine("\t- Logging out."); // TODO - This does not seem to work, a manual logout is recommended. vault.LogOutSilent(); // Try to take the vault on/off-line. try { Logger.WriteLine("\t- Taking Vault Offline"); serverApp.VaultManagementOperations.TakeVaultOffline(settings.VaultGuid, true); Logger.WriteLine("\t- Bringing Vault back Online"); serverApp.VaultManagementOperations.BringVaultOnline(settings.VaultGuid); Logger.WriteLine("Vault Now Online"); } catch (Exception ex) { // report the error Logger.WriteLine(ex.Message); } // TODO - I need to find a better way to close open client windows, but until then... if (settings.KillExplorerWindows) { Logger.WriteLine("Closing Open Explorer Windows."); foreach (Process p in Process.GetProcessesByName("explorer")) p.Kill(); } Logger.WriteLine(@"Waiting a few seconds..."); Thread.Sleep(1000 * 2); } // Determine if we should open the Vault Window for the Developer. if (Settings.OpenVault) { Logger.WriteLine(@"Opening Path: " + settings.LocalVaultPath); // Open the path in the default application. Process.Start(Settings.LocalVaultPath); } } } // Should we leave the console window open or not? if (!settings.AutoExitApp) { Logger.NewLine(); Logger.WriteLine(@"Press any key to exit..."); Console.ReadKey(); } }