SharePoint development-using session (Code modification Webconfig)

Source: Internet
Author: User

SharePoint Enable session can use PowerShell, poke here; Webconfig can be modified.

The focus of this narrative is to control the session opening and closing sessions via feature.

Create a new empty SharePoint project, write the processing logic for the Featureeventhandler featureactivated and featuredeactivating

public override void FeatureActivated (Spfeaturereceiverproperties properties) {spwebapplication Applica tion = (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) {SPW Ebapplication application = (spwebapplication) properties.            Feature.parent;                try {config.restore (application); Application. Farm.services.getvalue<spwebservice> ().            Applywebconfigmodifications (); } catch {}}
Here config is the processing class for the session, because enabling and disabling the session is essentially modifying the configuration of the IIS Web site, so get the WebApplication object and modify the Webconfig content through code.

The code for the Config class is as follows

Class Config {private static void Execupdate (Spwebapplication webApp, string name, string path, string value, M Icrosoft. SharePoint.Administration.SPWebConfigModification.SPWebConfigModificationType type) {spwebconfigmodific            ation 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") {con                Figmodificationstoremove.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 advantage of using the Spwebapplication object to record webconfigmodifications, or webconfig modifications, is that it is easy to restore the webconfig that were modified by the code.

SharePoint development-using session (Code modification Webconfig)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.