SharePoint offers the SPWebConfigModification class to update web.config entries. We can have a Feature receiver at the Web Application scope and on activation propgate the web config changes to all the WFE servers .. This is good right?
But the caveat is that the class is not so flexible. It does not allow us to reorder the entries within a section which is really important in case of custom Http modules ad handlers. Hope MS takes care of this in the next update ..Hers is a sample code to add the connection strings section and update the enableSessionstate attribute.
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
SPWebConfigModification wcupdate1 = new SPWebConfigModification();
wcupdate1.Owner = "Any Name";
wcupdate1.Name = "enableSessionState";
wcupdate1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
wcupdate1.Path = "configuration/system.web/pages";
wcupdate1.Value = "true";
// This is not required if the ConnectionStrings section has already been created in the web.config
SPWebConfigModification wcupdate3 = new SPWebConfigModification();
wcupdate3.Owner = "Any Name";
wcupdate3.Name = "connectionStrings";
wcupdate3.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
wcupdate3.Path = "configuration";
wcupdate3.Value = "";
string DBConnectionString = Read from a list or feature property (security reasons)
SPWebConfigModification wcupdate4 = new SPWebConfigModification();
wcupdate4.Owner = "Any Name";
wcupdate4.Name = "add[@name='ConnStringName']";
wcupdate4.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
wcupdate4.Path = "configuration/connectionStrings";
wcupdate4.Value = String.Format("", DBConnectionString); ;
webApp.WebConfigModifications.Add(wcupdate1);
webApp.WebConfigModifications.Add(wcupdate3);
webApp.WebConfigModifications.Add(wcupdate4);
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
webApp.Update();
Hope this helps
But the caveat is that the class is not so flexible. It does not allow us to reorder the entries within a section which is really important in case of custom Http modules ad handlers. Hope MS takes care of this in the next update ..Hers is a sample code to add the connection strings section and update the enableSessionstate attribute.
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
SPWebConfigModification wcupdate1 = new SPWebConfigModification();
wcupdate1.Owner = "Any Name";
wcupdate1.Name = "enableSessionState";
wcupdate1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
wcupdate1.Path = "configuration/system.web/pages";
wcupdate1.Value = "true";
// This is not required if the ConnectionStrings section has already been created in the web.config
SPWebConfigModification wcupdate3 = new SPWebConfigModification();
wcupdate3.Owner = "Any Name";
wcupdate3.Name = "connectionStrings";
wcupdate3.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
wcupdate3.Path = "configuration";
wcupdate3.Value = "
string DBConnectionString = Read from a list or feature property (security reasons)
SPWebConfigModification wcupdate4 = new SPWebConfigModification();
wcupdate4.Owner = "Any Name";
wcupdate4.Name = "add[@name='ConnStringName']";
wcupdate4.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
wcupdate4.Path = "configuration/connectionStrings";
wcupdate4.Value = String.Format("
webApp.WebConfigModifications.Add(wcupdate1);
webApp.WebConfigModifications.Add(wcupdate3);
webApp.WebConfigModifications.Add(wcupdate4);
webApp.Farm.Services.GetValue
webApp.Update();
Hope this helps
Comments
Post a Comment