2011-02-03 44 views
2

我有一个关于在功能冲压web.config文件中修改一个小问题在这里(SP2010 [Web应用程序级别功能,激活缺省])问题更改Web.config文件的SharePoint

我面临两个怪问题

  1. 像(添加子节点)应用的修改在web.config中出现多次。
  2. 在功能停用时,我将删除对所有者的修改,它获得修改,但是 这些不会被删除。

我在功能激活过程中使用了下面的代码片段。

ModificationEntry[] enries = 
{ 
    new ModificationEntry("someName", "someSection", SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode) 
}; 

    SPWebApplication WebApp = (SPWebApplication)properties.Feature.Parent; 
WebApp.WebConfigModifications.Clear(); 

foreach (ModificationEntry entry in enries) 
{ 
    // CreateModification simply return me SPWebConfigModification 
    SPWebConfigModification configModificationItem = CreateModification(entry, properties.Feature.DefinitionId.ToString()); 
    if (!WebApp.WebConfigModifications.Contains(configModificationItem)) 
    { 
     WebApp.WebConfigModifications.Add(configModificationItem); 
    } 
} 

WebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
WebApp.Update(); 

这就是我正在做的功能停用。

if (webApp != null) 
{ 
    Collection<SPWebConfigModification> collection = webApp.WebConfigModifications; 
    int iStartCount = collection.Count; 

    // Remove any modifications that were originally created by the owner. 
    for (int c = iStartCount - 1; c >= 0; c--) 
    { 
     SPWebConfigModification configMod = collection[c]; 
     if (configMod.Owner == properties.Feature.DefinitionId.ToString()) 
      collection.Remove(configMod); 
    } 

    // Apply changes only if any items were removed. 
    if (iStartCount > collection.Count) 
    { 
     webApp.Update(); 
     webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
    } 
} 

请评论!

+0

其中是ModificationEntry/CreateModification的代码。你似乎没有一个'路径'变量在任何地方(除非是someSection),但那么'价值' – djeeg 2011-02-03 22:18:16

回答

2
public sealed class EnableServiceAdapterFeatureReceiver : SPFeatureReceiver 
    { 
     private string psSiteUrl = ""; 

    #region Not Implemented 

    /// <summary> 
    /// Install of feature - not implemented 
    /// </summary> 
    /// <param name="properties"></param> 
    public override void FeatureInstalled(SPFeatureReceiverProperties properties) 
    { 
    } 

    /// <summary> 
    /// Uninstall of feature - not implemented 
    /// </summary> 
    /// <param name="properties"></param> 
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties) 
    { 
    } 

    #endregion 

    #region Base Class Overrides 

    /// <summary> 
    /// Activation of feature - adds modifications into web.config and change masterpage. 
    /// </summary> 
    /// <param name="properties"></param> 
    public override void FeatureActivated(SPFeatureReceiverProperties properties) 
    { 
     using (SPSite site = properties.Feature.Parent as SPSite) 
     { 
      psSiteUrl = site.Url; 

      SPWebApplication webApplication = site.WebApplication; 
      if (webApplication == null) 
      { 
       return; 
      } 

      Modification(webApplication, site.RootWeb.Title.Trim(), true); 

      webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
      webApplication.Update(); 
     } 
    } 

    /// <summary> 
    /// Deactivation of feature - removes modifications from web.config 
    /// </summary> 
    /// <param name="properties"></param> 
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
    { 

     using (SPSite site = properties.Feature.Parent as SPSite) 
     { 
      SPWebApplication webApplication = site.WebApplication; 
      if (webApplication == null) 
      { 
       return; 
      } 

      Modification(webApplication, site.RootWeb.Title.Trim(), false); 

      webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
      webApplication.Update(); 
     } 
    } 

    #endregion 

    #region Private Methods 

    /// <summary> 
    /// Modifies web.config file to add/remove SL Service Adapter support for the application 
    /// </summary> 
    /// <param name="webApplication"></param> 
    /// <param name="add"></param> 
    private void Modification(SPWebApplication webApplication, string webTitle, bool add) 
    { 
     // system.serviceModel 
     SPWebConfigModification svcModelConfigSection = new SPWebConfigModification(); 
     svcModelConfigSection.Name = "system.serviceModel"; 
     svcModelConfigSection.Path = "configuration"; 
     svcModelConfigSection.Value = "" + 
            "<system.serviceModel>" + 
            "<bindings>" + 
            "<netTcpBinding>" + 
            "<binding name=\"NetTcpBinding_IAdapterService\" closeTimeout=\"00:01:00\" openTimeout=\"00:01:00\" receiveTimeout=\"00:30:00\" sendTimeout=\"00:30:00\" transactionFlow=\"false\" transferMode=\"Buffered\" transactionProtocol=\"OleTransactions\" hostNameComparisonMode=\"StrongWildcard\" listenBacklog=\"10\" maxBufferPoolSize=\"524288\" maxBufferSize=\"65536\" maxConnections=\"10\" maxReceivedMessageSize=\"10240000\">" + 
            "<readerQuotas maxDepth=\"32\" maxStringContentLength=\"8192\" maxArrayLength=\"16384\" maxBytesPerRead=\"4096\" maxNameTableCharCount=\"16384\" />" + 
            "<reliableSession ordered=\"true\" inactivityTimeout=\"00:10:00\" enabled=\"false\" />" + 
            "<security mode=\"Transport\">" + 
            "<transport clientCredentialType=\"Windows\" protectionLevel=\"None\" />" + 
            "</security>" + 
            "</binding>" + 
            "<binding name=\"tcp_Unsecured\">" + 
            "<security mode=\"None\" />" + 
            "</binding>" + 
            "</netTcpBinding>" + 
            "</bindings>" + 
            "<client>" + 
            "<endpoint address=\"net.tcp://slrsptm03.curie.sl.se/AdapterService/AdapterService\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"NetTcpBinding_IAdapterService\">" + 
            "</endpoint>" + 
            "<endpoint address=\"net.tcp://slrsptm04.curie.sl.se:8890/slHafas\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"HafasControllerService\" />" + 
            "<!-- Endpoint adress for SL Client to MobileAdapter.ServiceController WCF service-->" + 
            "<endpoint address=\"net.tcp://slrsptm04.curie.sl.se:8889/slMobile\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"MobileControllerService\" />" + 
            "</client>" + 
            "<diagnostics>" + 
            "<!-- Enable Message Logging here. -->" + 
            "<!-- log all messages received or sent at the transport or service model levels -->" + 
            "<messageLogging logEntireMessage=\"true\" maxMessagesToLog=\"65000\" logMessagesAtServiceLevel=\"true\" logMalformedMessages=\"true\" logMessagesAtTransportLevel=\"true\" />" + 
            "</diagnostics>" + 
            "</system.serviceModel>"; 
     svcModelConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; 

     // appSettings 
     SPWebConfigModification appSettingsConfigSection = new SPWebConfigModification(); 
     appSettingsConfigSection.Name = "appSettings"; 
     appSettingsConfigSection.Path = "configuration"; 
     appSettingsConfigSection.Value = "" + 
            "<appSettings>" + 
            "<add key=\"MainUrl\" value=\"" + psSiteUrl + "/Planeradtrafik/default.aspx\" />" + 
            "<add key=\"TDIUrl\" value=\"" + psSiteUrl + "/TDI/default.aspx\" />" + 
            "<add key=\"HAFASUrl\" value=\"" + psSiteUrl + "/HAFAS/default.aspx\" />" + 
            "<add key=\"MOBILEUrl\" value=\"" + psSiteUrl + "/MOBILE/default.aspx\" />" + 
            "<add key=\"SessionTimeOut\" value=\"10\" />" + 
            "<add key=\"HitPageUrl\" value=\"" + psSiteUrl + "/hitpage.html\" />" + 
            "<add key=\"SPGroup\" value=\"" + webTitle + " Members;" + webTitle + " Visitors\" />" + 
            "</appSettings>"; 
     appSettingsConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; 

     // connectionStrings 
     SPWebConfigModification connStringConfigSection = new SPWebConfigModification(); 
     connStringConfigSection.Name = "connectionStrings"; 
     connStringConfigSection.Path = "configuration"; 
     connStringConfigSection.Value = "" + 
            "<connectionStrings>" + 
            "<add name=\"SL_Portal_DBConnectionString\" connectionString=\"Data Source=.;Initial Catalog=SL_Portal_DB;Integrated Security=false;user id=sl_portal_db_user;password=[password]\" providerName=\"System.Data.SqlClient\" />" + 
            "</connectionStrings>"; 
     connStringConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; 

     // httpModules 
     SPWebConfigModification httpModuleConfigSection = new SPWebConfigModification(); 
     httpModuleConfigSection.Name = "add[@name='Session']"; 
     httpModuleConfigSection.Path = "configuration/system.web/httpModules"; 
     httpModuleConfigSection.Value = 
      "<add name=\"Session\" type=\"System.Web.SessionState.SessionStateModule\"/>"; 
     httpModuleConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; 

     if (add) 
     { 
      webApplication.WebConfigModifications.Add(svcModelConfigSection); 
      webApplication.WebConfigModifications.Add(appSettingsConfigSection); 
      webApplication.WebConfigModifications.Add(connStringConfigSection); 
      webApplication.WebConfigModifications.Add(httpModuleConfigSection); 
     } 
     else 
     { 
      webApplication.WebConfigModifications.Add(httpModuleConfigSection); 
      webApplication.WebConfigModifications.Remove(connStringConfigSection); 
      webApplication.WebConfigModifications.Remove(appSettingsConfigSection); 
      webApplication.WebConfigModifications.Remove(svcModelConfigSection); 
     } 
    } 

    #endregion 
} 
0

该类具有所有必需的属性,它也包含Value。

ModificationEntry("someName", "someSection", “Value”, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode) 

这不是问题,我可以将配置项添加到web.config文件。问题如上所述。

1

由于您没有发布CreateModification()的代码,我必须猜测它,但我认为那里有些问题。

SPWebConfigModification的Name属性值是正确的XPath非常重要。假如这是你想要什么:

添加[键=“testadd”]

这看上去很好,但事实并非如此。由于“钥匙”是一个属性,比如“名”和其他人,这是正确的做法:

添加[@键=“testadd”]

这导致多个条目,使修改无法删除。