SharePoint development, sharepoint

Source: Internet
Author: User

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.

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.