SharePoint development, sharepoint
To enable Session in SharePoint, you can use Powershell to stamp it here. You can modify webconfig.
The focus of this article is to use feature to control enabling and closing sessions.
Create an empty SharePoint project and write the processing logic of FeatureEventHandler FeatureActivated and FeatureDeactivating.
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWebApplication application = (SPWebApplication)properties.Feature.Parent; try { Config.Update(application); application.Update(); application.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); } catch { Config.Restore(application); } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPWebApplication application = (SPWebApplication)properties.Feature.Parent; try { Config.Restore(application); application.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); } catch { } }
Here, Config is about the Session processing class. Because the Enable and disable Session is essentially modifying the configuration of the IIS website, the WebApplication object is obtained and the webconfig content is modified through code.
The Config class code is as follows:
class Config { private static void ExecUpdate(SPWebApplication webApp, string name, string path, string value, Microsoft.SharePoint.Administration.SPWebConfigModification.SPWebConfigModificationType type) { SPWebConfigModification modification = new SPWebConfigModification(); modification.Name = name; modification.Path = path; modification.Value = value; modification.Sequence = 0; modification.Type = type; modification.Owner = "SP_MIP_Team_EnableSession"; SPWebConfigModification item = modification; webApp.WebConfigModifications.Add(item); } public static void Restore(SPWebApplication webApp) { webApp.Update(); List<SPWebConfigModification> configModificationsToRemove = new List<SPWebConfigModification>(); foreach (SPWebConfigModification spWebConfigModification in webApp.WebConfigModifications) { if (spWebConfigModification.Owner == "SP_MIP_Team_EnableSession") { configModificationsToRemove.Add(spWebConfigModification); } } foreach (SPWebConfigModification configModification in configModificationsToRemove) { webApp.WebConfigModifications.Remove(configModification); } webApp.Update(); } public static void Update(SPWebApplication webApp) { string name = null; string path = null; string value = null; name = "add[@name='Session'][@type='System.Web.SessionState.SessionStateModule']"; path = "/configuration/system.web/httpModules"; value = "<add name='Session' type='System.Web.SessionState.SessionStateModule' />"; ExecUpdate(webApp, name, path, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode); name = "enableSessionState"; path = "/configuration/system.web/pages"; value = "true"; ExecUpdate(webApp, name, path, value, SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute); name = "add[@name='session'][@type='System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a']"; path = "/configuration/system.webServer/modules"; value = "<add name='session' type='System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />"; ExecUpdate(webApp, name, path, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode); } }
The SPWebApplication object is used to record WebConfigModifications, that is, webconfig modification. The advantage of this is that webconfig modified through code can be easily restored.