码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-12-19 10:07:25      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:sharepoint

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开发 - 使用Session(代码修改webconfig)

标签:sharepoint

原文地址:http://blog.csdn.net/foxdave/article/details/42023827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!