SharePoint开发 - 使用Session(代码修改webconfig)

博客地址 http://blog.csdn.net/foxdave

SharePoint启用Session可以使用Powershell,戳这里;可以修改webconfig。

本篇叙述的重点是通过feature去控制启用session和关闭session。

新建一个空的SharePoint项目,编写FeatureEventHandler的FeatureActivated和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
            {
            }
        }
这里Config是关于Session的处理类,因为启用和禁用Session本质上还是修改IIS网站的配置,所以获取WebApplication对象,通过代码修改webconfig内容。

Config类的代码如下

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);
        }

    }
这里利用SPWebApplication对象来记录WebConfigModifications,即webconfig的修改,这样做的好处是可以方便地还原通过代码修改的webconfig

你可能感兴趣的:(SharePoint)