private void Delete_Click(object sender, RoutedEventArgs e) { try { if (SelectedConnection == null) { return; } if (string.IsNullOrEmpty(SelectedConnection.ConnectionString)) { return; } var path = Path.GetDirectoryName(SelectedProject.FullName); if (!SharedConfigFile.ConfigFileExists(SelectedProject)) { _logger.WriteToOutputWindow("Error Deleting Connection: Missing CRMDeveloperExtensions.config File", Logger.MessageType.Error); return; } MessageBoxResult result = MessageBox.Show("Are you sure?" + Environment.NewLine + Environment.NewLine + "This will delete the connection information and all associated mappings.", "Delete Connection", MessageBoxButton.YesNo); if (result != MessageBoxResult.Yes) { return; } if (!SharedConfigFile.ConfigFileExists(SelectedProject)) { return; } //Delete Connection XmlDocument doc = new XmlDocument(); doc.Load(path + "\\CRMDeveloperExtensions.config"); XmlNodeList connections = doc.GetElementsByTagName("Connection"); if (connections.Count == 0) { return; } List <XmlNode> nodesToRemove = new List <XmlNode>(); foreach (XmlNode connection in connections) { XmlNode orgId = connection["OrgId"]; if (orgId == null) { continue; } if (orgId.InnerText.ToUpper() != SelectedConnection.OrgId.ToUpper()) { continue; } nodesToRemove.Add(connection); } foreach (XmlNode xmlNode in nodesToRemove) { if (xmlNode.ParentNode != null) { xmlNode.ParentNode.RemoveChild(xmlNode); } } if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config")) { FileInfo file = new FileInfo(path + "\\CRMDeveloperExtensions.config") { IsReadOnly = false }; } doc.Save(path + "\\CRMDeveloperExtensions.config"); //Delete related Files doc.Load(path + "\\CRMDeveloperExtensions.config"); string type; switch (SourceWindow) { case "PluginDeployer": type = "Assembly"; break; case "SolutionPackager": type = "Solution"; break; default: //WebResourceDeployer or ReportDeployer type = "File"; break; } XmlNodeList mapping = doc.GetElementsByTagName(type); if (mapping.Count > 0) { nodesToRemove = new List <XmlNode>(); foreach (XmlNode fileNode in mapping) { XmlNode orgId = fileNode["OrgId"]; if (orgId == null) { continue; } if (orgId.InnerText.ToUpper() != SelectedConnection.OrgId.ToUpper()) { continue; } nodesToRemove.Add(fileNode); } foreach (XmlNode xmlNode in nodesToRemove) { if (xmlNode.ParentNode != null) { xmlNode.ParentNode.RemoveChild(xmlNode); } } doc.Save(path + "\\CRMDeveloperExtensions.config"); } GetConnections(); OnConnectionDeleted(); } catch (Exception ex) { _logger.WriteToOutputWindow("Error Deleting Connection: Missing CRMDeveloperExtensions.config File: " + ex.Message + Environment.NewLine + ex.StackTrace, Logger.MessageType.Error); } }
private void AddOrUpdateConnection(Project vsProject, string connectionName, string connString, string orgId, string versionNum, bool showPrompt) { try { var path = Path.GetDirectoryName(vsProject.FullName); if (!SharedConfigFile.ConfigFileExists(vsProject)) { _logger.WriteToOutputWindow("Error Adding Or Updating Connection: Missing CRMDeveloperExtensions.config File", Logger.MessageType.Error); return; } FileInfo file = new FileInfo(path + "\\CRMDeveloperExtensions.config"); XmlDocument doc = new XmlDocument(); doc.Load(path + "\\CRMDeveloperExtensions.config"); //Check if connection already exists for project XmlNodeList connectionNodes = doc.GetElementsByTagName("Connection"); if (connectionNodes.Count > 0) { foreach (XmlNode node in connectionNodes) { XmlNode nameNode = node["Name"]; if (nameNode != null && nameNode.InnerText != connectionName) { continue; } if (showPrompt) { MessageBoxResult result = MessageBox.Show("Update Connection?", "Connection Already Added", MessageBoxButton.YesNo); //Update existing connection if (result != MessageBoxResult.Yes) { return; } } bool changed = false; if (nameNode != null) { string oldConnectionName = nameNode.InnerText; if (oldConnectionName != connectionName) { nameNode.InnerText = connectionName; changed = true; } } XmlNode versionNode = node["Version"]; if (versionNode != null) { string oldVersionNum = versionNode.InnerText; if (oldVersionNum != versionNum) { versionNode.InnerText = versionNum; changed = true; } } XmlNode connectionStringNode = node["ConnectionString"]; if (connectionStringNode != null) { string oldConnectionString = connectionStringNode.InnerText; string encodedConnectionString = EncodeString(connString); if (oldConnectionString != encodedConnectionString) { connectionStringNode.InnerText = encodedConnectionString; changed = true; } } if (!changed) { return; } if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config")) { file.IsReadOnly = false; } doc.Save(path + "\\CRMDeveloperExtensions.config"); return; } } //Add the connection elements XmlNodeList connections = doc.GetElementsByTagName("Connections"); XmlElement connection = doc.CreateElement("Connection"); XmlElement name = doc.CreateElement("Name"); name.InnerText = connectionName; connection.AppendChild(name); XmlElement org = doc.CreateElement("OrgId"); org.InnerText = orgId; connection.AppendChild(org); XmlElement connectionString = doc.CreateElement("ConnectionString"); connectionString.InnerText = EncodeString(connString); connection.AppendChild(connectionString); XmlElement version = doc.CreateElement("Version"); version.InnerText = versionNum; connection.AppendChild(version); connections[0].AppendChild(connection); _connectionAdded = true; if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config")) { file.IsReadOnly = false; } doc.Save(path + "\\CRMDeveloperExtensions.config"); } catch (Exception ex) { _logger.WriteToOutputWindow("Error Adding Or Updating Connection: " + ex.Message + Environment.NewLine + ex.StackTrace, Logger.MessageType.Error); } }