private void UnregisterHttpModule(SPFeatureReceiverProperties properties) { SPWebConfigModification webConfigModification = CreateWebModificationObject(); SPSecurity.RunWithElevatedPrivileges(() => { SPWebService contentService = properties.Definition.Farm.Services.GetValue <SPWebService>(); int numberOfModifications = contentService.WebConfigModifications.Count; //Iterate over all WebConfigModification and delete only those we have created for (int i = numberOfModifications - 1; i >= 0; i--) { SPWebConfigModification currentModifiction = contentService.WebConfigModifications[i]; if (currentModifiction.Owner.Equals(webConfigModification.Owner)) { contentService.WebConfigModifications.Remove(currentModifiction); } } //Update only if we have something deleted if (numberOfModifications > contentService.WebConfigModifications.Count) { contentService.Update(); contentService.ApplyWebConfigModifications(); } }); }
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { try { SPWebService service = SPWebService.ContentService; Collection <SPWebConfigModification> modsCollection = service.WebConfigModifications; // Find the most recent modification of a specified owner int modsCount1 = modsCollection.Count; for (int i = modsCount1 - 1; i > -1; i--) { //I will remove from the web.config just the lines related to this solution and not the ones deployed from other solutions. if (modsCollection[i].Owner.Equals("DH")) { modsCollection.Remove(modsCollection[i]); } } // Save web.config changes. service.Update(); // Applies the list of web.config modifications to all Web applications in this Web service across the farm. service.ApplyWebConfigModifications(); } catch (Exception exception) { //This is a logging class I use to write exceptions to the ULS Logs. There are plenty of options in this area as well. ULSLog2013.LogError(exception); throw; } }
private static void UpdateService(SPWebApplication app) { app.Update(true); SPWebService service = app.Farm.Services.GetValue <SPWebService>(); //service.Update(); service.ApplyWebConfigModifications(); }
private void RegisterHttpModule(SPFeatureReceiverProperties properties) { SPWebConfigModification webConfigModification = CreateWebModificationObject(); SPSecurity.RunWithElevatedPrivileges(() => { SPWebService contentService = SPWebService.ContentService; contentService.WebConfigModifications.Add(webConfigModification); contentService.Update(); contentService.ApplyWebConfigModifications(); }); }
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { try { SPWebService contentService = SPWebService.ContentService; contentService.WebConfigModifications.Remove(GetConfigModification()); // Serialize the web application state and propagate changes across the farm. contentService.Update(); // Save web.config changes. contentService.ApplyWebConfigModifications(); } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }
// Uncomment the method below to handle the event raised before a feature is deactivated. public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSecurity.RunWithElevatedPrivileges(() => { SPWebService service = SPWebService.ContentService; Collection <SPWebConfigModification> modsCollection = service.WebConfigModifications; int modsCount1 = modsCollection.Count; for (int i = modsCount1 - 1; i > -1; i--) { if (modsCollection[i].Owner.Equals("TopNavCustomSiteMapProvider")) { modsCollection.Remove(modsCollection[i]); } } service.Update(); service.ApplyWebConfigModifications(); }); }
// Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSecurity.RunWithElevatedPrivileges(() => { SPWebService service = SPWebService.ContentService; SPWebConfigModification myModification = new SPWebConfigModification(); myModification.Path = "configuration/system.web/siteMap/providers"; myModification.Name = "add[@name='TestPortalSiteMapNonPublishingSite']"; myModification.Sequence = 0; myModification.Owner = "TopNavCustomSiteMapProvider"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; var typeName = typeof(TestPortalSiteMapNonPublishingSite.Navigation).FullName + ", " + typeof(TestPortalSiteMapNonPublishingSite.Navigation).Assembly.FullName; myModification.Value = "<add name=\"TestPortalSiteMapNonPublishingSite\" type=\"" + typeName + "\" NavigationType=\"Global\" />"; service.WebConfigModifications.Add(myModification); service.Update(); service.ApplyWebConfigModifications(); }); }
// Uncomment the method below to handle the event raised after a feature has been activated. //public override void FeatureActivated(SPFeatureReceiverProperties properties) //{ //} public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { ULSLog2013.LogWarning("Starting feature activation"); SPWebService service = SPWebService.ContentService; //There is another SPWebConfigModification constructor accepting 2 parameters: //name and xpath, but I think it is cleaner from the reader's perspective to do it in separate lines. SPWebConfigModification myModification = new SPWebConfigModification(); //xPath to where we want to add our node. In order to avoid weird errors, consider this as key sensitive. myModification.Path = "configuration/system.web/siteMap/providers"; myModification.Name = "add[@name='MyCustomNavigationProvider']"; myModification.Sequence = 0; //The owner property will help us to categorize and clean just our own customizations when the feature will be deactivated. //You can choose a more professional naming convention to identify your changes to web.config. myModification.Owner = "DH"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Value = "<add name=\"MyCustomNavigationProvider\" type=\"TopNav.MyCustomSiteMapProvider, TopNav, Version=1.0.0.0, Culture=neutral, PublicKeyToken=912f3c6491be12c2\" NavigationType=\"Global\" />"; //You are adding a SPWebConfigModification to the collection of modifications. //Every time you run this code a new item will be added to it. service.WebConfigModifications.Add(myModification); /*Call Update and ApplyWebConfigModifications to save changes*/ //If there is an error in any of the elements to add/modify in the web.config, it will produce an error and your line will not be added. //This collection works as a FIFO queue and therefore, you could be adding more and more elements to it. service.Update(); service.ApplyWebConfigModifications(); } catch (Exception exception) { //This is a logging class I use to write exceptions to the ULS Logs. There are plenty of options in this area as well. //Logging.WriteExceptionToTraceLog(exception.Source, exception.Message, exception, null); throw; } }
/// <summary> /// Make modifications to the web.config when the feature is activated. /// </summary> /// <param name="properties"></param> public override void FeatureActivated(SPFeatureReceiverProperties properties) { spSite = (SPSite)properties.Feature.Parent; spWeb = spSite.OpenWeb(); spWebService = SPWebService.ContentService; Configuration config = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name); string smtpServer = string.Empty; string configFilePath = config.FilePath; doc = new XmlDocument(); doc.Load(configFilePath); AppSettingsSection appSettings = config.AppSettings; if (appSettings.ElementInformation.IsPresent) { nodeSmtpServer = doc.SelectSingleNode("configuration/appSettings/add[@key='smtpServer']"); nodeUserName = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignUserName']"); nodePassword = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignPassword']"); // Add smtpServer to web.config file if(nodeSmtpServer == null) ModifyWebConfigData("add[@key='SMTPServer']", "configuration/appSettings", "<add key='smtpServer' value='" + SMTP_SERVER + "' />"); // Add DocuSign Service user Credentials to web.config file if (nodeUserName == null) ModifyWebConfigData("add[@key='userName']", "configuration/appSettings", "<add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' />"); // Add Profiles to web.config file if(nodePassword == null) ModifyWebConfigData("add[@key='password']", "configuration/appSettings", "<add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' />"); } else ModifyWebConfigData("add[@key='SMTPServer']", "configuration", "<appSettings><add key='smtpServer' value='" + SMTP_SERVER + "' /><add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' /><add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' /></appSettings>"); nodeServiceModel = doc.SelectSingleNode("configuration/system.serviceModel"); // Add ServiceModel to web.config file if(nodeServiceModel == null) ModifyWebConfigData("ServiceModel", "configuration", GetServiceModelTag()); XmlNode nodeTrust = null; XmlNode nodeEnableSessionState = null; nodeTrust = doc.SelectSingleNode("configuration/system.web/trust"); nodeEnableSessionState = doc.SelectSingleNode("configuration/system.web/pages"); if (nodeEnableSessionState != null) { XmlAttribute attributeEnableSessionState = nodeEnableSessionState.Attributes["enableSessionState"]; if (attributeEnableSessionState != null) { attributeEnableSessionState.Value = "true"; } } // Set the level attribute to WSS_Minimal if (nodeTrust != null) { XmlAttribute attributeLevel = nodeTrust.Attributes["level"]; if (attributeLevel != null) { attributeLevel.Value = "Full"; } } doc.Save(configFilePath); ModifyWebConfigData("add[@name='Session']", "configuration/system.web/httpModules", "<add name='Session' type='System.Web.SessionState.SessionStateModule' />"); spWebService.Update(); spWebService.ApplyWebConfigModifications(); }
/// <summary> /// Make modifications to the web.config when the feature is activated. /// </summary> /// <param name="properties"></param> public override void FeatureActivated(SPFeatureReceiverProperties properties) { spSite = (SPSite)properties.Feature.Parent; spWeb = spSite.OpenWeb(); spWebService = SPWebService.ContentService; Configuration config = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name); string smtpServer = string.Empty; string configFilePath = config.FilePath; doc = new XmlDocument(); doc.Load(configFilePath); AppSettingsSection appSettings = config.AppSettings; if (appSettings.ElementInformation.IsPresent) { nodeSmtpServer = doc.SelectSingleNode("configuration/appSettings/add[@key='smtpServer']"); nodeUserName = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignUserName']"); nodePassword = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignPassword']"); // Add smtpServer to web.config file if (nodeSmtpServer == null) { ModifyWebConfigData("add[@key='SMTPServer']", "configuration/appSettings", "<add key='smtpServer' value='" + SMTP_SERVER + "' />"); } // Add DocuSign Service user Credentials to web.config file if (nodeUserName == null) { ModifyWebConfigData("add[@key='userName']", "configuration/appSettings", "<add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' />"); } // Add Profiles to web.config file if (nodePassword == null) { ModifyWebConfigData("add[@key='password']", "configuration/appSettings", "<add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' />"); } } else { ModifyWebConfigData("add[@key='SMTPServer']", "configuration", "<appSettings><add key='smtpServer' value='" + SMTP_SERVER + "' /><add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' /><add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' /></appSettings>"); } nodeServiceModel = doc.SelectSingleNode("configuration/system.serviceModel"); // Add ServiceModel to web.config file if (nodeServiceModel == null) { ModifyWebConfigData("ServiceModel", "configuration", GetServiceModelTag()); } XmlNode nodeTrust = null; XmlNode nodeEnableSessionState = null; nodeTrust = doc.SelectSingleNode("configuration/system.web/trust"); nodeEnableSessionState = doc.SelectSingleNode("configuration/system.web/pages"); if (nodeEnableSessionState != null) { XmlAttribute attributeEnableSessionState = nodeEnableSessionState.Attributes["enableSessionState"]; if (attributeEnableSessionState != null) { attributeEnableSessionState.Value = "true"; } } // Set the level attribute to WSS_Minimal if (nodeTrust != null) { XmlAttribute attributeLevel = nodeTrust.Attributes["level"]; if (attributeLevel != null) { attributeLevel.Value = "Full"; } } doc.Save(configFilePath); ModifyWebConfigData("add[@name='Session']", "configuration/system.web/httpModules", "<add name='Session' type='System.Web.SessionState.SessionStateModule' />"); spWebService.Update(); spWebService.ApplyWebConfigModifications(); }