转摘:更改SharePoint 的web.config设置的两种方式

原文地址:http://www.cnblogs.com/ruijian/archive/2010/11/11/1875036.html

参考:http://msdn.microsoft.com/zh-cn/library/ms460914.aspx

MSDN上写的不是很明白,自己测试通过,记录一下;

方法一:通过创建描述一组 Web.config 修改的 XML 文件以声明方式注册 Web.config 项。

备注:

直接写一个webconfig.**.xml到14文件夹下的CONFIG中,在运行stsadm -o copyappbincontent 效果是一样的;

方法一有个缺点,会把所有WebApplication的web.Config都修改掉;

1.在sharepoint解决方案里添加14文件夹下的CONFIG文件夹,新建webconfig.**.xml文件(本文中使用的是webconfig.rj.xml),编辑内容;

2.修改解决方案包,如下;

  

3.添加后期部署命令行,如果解决方案里没有这一步,也可以直接在cmd中直接运行;

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Bin\stsadm.exe" -o copyappbincontent

方法二:使用 SharePoint Foundation 对象模型来修改 Web.config 设置

备注:

必须为WebApplication以上范围的feature,不能是Site或web范围;

2011-01-06:不能在site scope feature使用的原因找到了,是因为SPWebService.RemoteAdministratorAccessDenied默认为TRUE,将下表中的脚本保存为Set-RemoteAdministratorAccessDenied-False.ps,以管理员身份运行后IISreset,就OK了;

参考:http://unclepaul84.blogspot.com/2010/06/sppersistedobject-xxxxxxxxxxx-could-not.html (过墙)

# AUTHOR: Paul Kotlyar

# CONTACT: unclepaul84@gmail.com

# DESCRIPTION: sets an option on content web service that allows updating of SP Administration objects such as SPJobDefinition from content web applications

function Set-RemoteAdministratorAccessDenied-False()

{

    # load sharepoint api libs

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") > $null

 

# get content web service

$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

# turn off remote administration security

$contentService.RemoteAdministratorAccessDenied = $false

# update the web service

$contentService.Update()

    

}

 

Set-RemoteAdministratorAccessDenied-False

 

缺点是如果上级节点不存在,就没有办法添加SPWebConfigModification实例:比如要在"configuration/connectionStrings"下添加一个"<add name='tongji' connectionString='(请自行修改)Data Source=(loca…….",因为wenconfig默认不存在"configuration/connectionStrings"节点,所以部署时会报错;

  1. 确定Featrue范围,添加事件接收器

2.参考一下URL编写代码

http://didierdanse.net/blogs/dev_en/archive/2009/09/11/sharepoint-how-to-use-spwebconfigmodification-class.aspx

  

 

 

2011-3-3  在管理中心修改节点,为了在2010下使用请修改WSP安装的webconfig.aspx  把12替换成14就行

if you want to modify a value without executing any code, you will have to use a page that is able to manage all the entries. You can find such a page here: http://blog.thekid.me.uk/archive/2007/03/24/web-config-modification-manager-for-sharepoint.aspx

 

http://www.codeproject.com/KB/sharepoint/SPWebConfigModTool.aspx?msg=3344238

2011-3-4  

遇到个问题,开启Session的情况下,出来以下错误,使用命令行“disable-SPSessionStateService”关闭Session后,恢复正常,原理未明

image

未能连接到配置数据库。

已引发类型为 System.Xml.XmlException 的异常。其他异常信息: “Session”是一个意外标记。应为空格。 行 1,位置 10668。

System.Xml.XmlException: “Session”是一个意外标记。应为空格。 行 1,位置 10668。

在 System.Xml.XmlTextReaderImpl.Throw(Exception e)

在 System.Xml.XmlTextReaderImpl.ParseAttributes()

在 System.Xml.XmlTextReaderImpl.ParseElement()

在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()

在 System.Xml.XmlLoader.ParsePartialContent(XmlNode parentNode, String innerxmltext, XmlNodeType nt)

在 System.Xml.XmlLoader.LoadInnerXmlElement(XmlElement node, String innerxmltext)

在 Microsoft.SharePoint.Administration.SPWebConfigFileChanges.ApplyModificationsWebConfigXmlDocument(XmlDocument xdWebConfig, String filepath)

在 Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications()

在 Microsoft.SharePoint.Administration.SPWebApplication.Provision()

在 Microsoft.SharePoint.Administration.SPWebServiceInstance.Provision()

在 Microsoft.SharePoint.Administration.SPFarm.Join()

在 Microsoft.SharePoint.PostSetupConfiguration.ConfigurationDatabaseTask.CreateOrConnectConfigDb()

在 Microsoft.SharePoint.PostSetupConfiguration.ConfigurationDatabaseTask.Run()

在 Microsoft.SharePoint.PostSetupConfiguration.TaskThread.ExecuteTask()

                            [XmlException: “Session”是一个意外标记。应为空格。 行 1,位置 10662。] 

                               System.Xml.XmlTextReaderImpl.Throw(Exception e) +95 

                               System.Xml.XmlTextReaderImpl.ParseAttributes() +5090073 

                               System.Xml.XmlTextReaderImpl.ParseElement() +378 

                               System.Xml.XmlTextReaderImpl.ParseDocumentContent() +429 

                               System.Xml.XmlLoader.ParsePartialContent(XmlNode parentNode, String innerxmltext, XmlNodeType nt) +253 

                               System.Xml.XmlLoader.LoadInnerXmlElement(XmlElement node, String innerxmltext) +30 

                               Microsoft.SharePoint.Administration.SPWebConfigFileChanges.ApplyModificationsWebConfigXmlDocument(XmlDocument xdWebConfig, String filepath) +1813 

                               Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications() +1455 

                               Microsoft.SharePoint.Administration.SPWebService.ApplyWebConfigModifications() +550 

                               ASP._admin_webconfig_aspx.DeleteSelected(Object sender, EventArgs e) +432 

                               System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115 

                               System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140 

                               System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29 

                             System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

                                                      Event code: 3005

                                                      Event message: 发生了未处理的异常。

                                                      Event time: 2011/3/3 20:51:12

                                                      Event time (UTC): 2011/3/3 12:51:12

                                                      Event ID: 03608a083d2c451fadeefd27ff97d1f8

                                                      Event sequence: 451

                                                      Event occurrence: 1

                                                      Event detail code: 0

                                                      Application information:

                                                      Application domain: /LM/W3SVC/89003312/ROOT-1-129436299874570910

                                                      Trust level: WSS_Minimal

                                                      Application Virtual Path: /

                                                      Application Path: C:\inetpub\wwwroot\wss\VirtualDirectories\10935\

                                                      Machine name: GAS-MOSSC

                                                      Process information:

                                                      Process ID: 6132

                                                      Process name: w3wp.exe

                                                      Account name: GAS\gasmoss

                                                      Exception information:

                                                      Exception type: XmlException

                                                      Exception message: “Session”是一个意外标记。应为空格。 行 1,位置 10662。

                                                      Request information:

                                                      Request URL: http://gas-mossc:2010/_admin/webconfig.aspx

                                                      Request path: /_admin/webconfig.aspx

                                                      User host address: ::1

                                                      User: GAS\gasmoss

                                                      Is authenticated: True

                                                      Authentication Type: NTLM

                                                      Thread account name: GAS\gasmoss

                                                      Thread information:

                                                      Thread ID: 8

                                                      Thread account name: GAS\gasmoss

                                                      Is impersonating: False

                                                      Stack trace: 在 System.Xml.XmlTextReaderImpl.Throw(Exception e)

                                                      在 System.Xml.XmlTextReaderImpl.ParseAttributes()

                                                      在 System.Xml.XmlTextReaderImpl.ParseElement()

                                                      在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()

                                                      在 System.Xml.XmlLoader.ParsePartialContent(XmlNode parentNode, String innerxmltext, XmlNodeType nt)

                                                      在 System.Xml.XmlLoader.LoadInnerXmlElement(XmlElement node, String innerxmltext)

                                                      在 Microsoft.SharePoint.Administration.SPWebConfigFileChanges.ApplyModificationsWebConfigXmlDocument(XmlDocument xdWebConfig, String filepath)

                                                      在 Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications()

                                                      在 Microsoft.SharePoint.Administration.SPWebService.ApplyWebConfigModifications()

                                                      在 ASP._admin_webconfig_aspx.DeleteSelected(Object sender, EventArgs e)

                                                      在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)

                                                      在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

                                                      在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

                                                      在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

                                                      Custom event details:

                                                      2011-03-08

                                                      发现name与value要对应,不然不会移除,如key对应key ,name对应name

                                                      private const string SPWebConfigModificationOwner = "1d206cbc-2a9c-4177-88c8-f384aceb3d9c";
                                                              public override void FeatureActivated(SPFeatureReceiverProperties properties)
                                                              {
                                                                  string name, xpath, value;
                                                                  SPWebApplication webApp = (properties.Feature.Parent as SPSite).WebApplication;
                                                                  #region ..: appSettings :..
                                                      
                                                                  name = "add[@name='SSOMapProvider']";//此处的@后面的值必须与value里的标识对应,不然RemoveAllModifications无法移除            xpath = "configuration/system.web/siteMap/providers";
                                                                  value = "<add name='SSOMapProvider' type='gas_SSOMapProvider.SSOMapProvider,gas_SSOMapProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc8ee07059daf82d' NavigationType='Current' />";
                                                                  SPWebConfig.ModifyWebConfig(webApp, SPWebConfigModificationOwner, name, xpath, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
                                                      
                                                                 
                                                                  /* 参?考?
                                                                   http://didierdanse.net/blogs/dev_en/archive/2009/09/11/sharepoint-how-to-use-spwebconfigmodification-class.aspx 
                                                                   name = "add[@key='KeyName']";
                                                                   xpath = "configuration/appSettings";
                                                                   value = "<add key='KeyName' value='Value' />";
                                                                   ModifyWebConfig(webApp, name, xpath, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
                                                                  */
                                                                  #endregion
                                                                  // 把此段放到调用里,避免用户已经配置的信息丢失                        try
                                                                  {
                                                                      webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                                                                  }
                                                                  catch (Exception ex)
                                                                  {
                                                                      SPWebConfig.RemoveAllModifications(webApp, SPWebConfigModificationOwner);
                                                                      throw ex;
                                                                  }
                                                              }
                                                              public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
                                                              {
                                                                  //回?收?后ó删?除y配?置?
                                                                  SPWebApplication webApp = (properties.Feature.Parent as SPSite).WebApplication;
                                                                  SPWebConfig.RemoveAllModifications(webApp, SPWebConfigModificationOwner);
                                                                  try
                                                                  {
                                                                      webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                                                                  }
                                                                  catch (Exception ex)
                                                                  {
                                                                      System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
                                                                      eventLog.Source = SPWebConfigModificationOwner;
                                                                      eventLog.WriteEntry(ex.Message);
                                                                      throw ex;
                                                                  }
                                                              }
                                                      

                                                      posted @ 2011-10-27 15:02  dong.net  阅读(1277)  评论(0)    收藏  举报