Following up on a previous post, How to Remove a web.config Modification Using PowerShell, in this post, we’ll demonstrate the opposite challenge: How do I add something to the web.config using PowerShell?
Many Bamboo products implement Telerik controls and install a specific version of Telerik.Web.UI assembly to the SharePoint farm. If the farm already has a different version of Telerik.Web.UI assembly, this will cause a conflict. To resolve this problem, we need to add an assembly binding to the web.config to specify which version to use. However, adding the text manually is not recommended because it is not synced across the servers in the farm and will be lost if the SPWebConfigModification class is called (since that manual change is not saved in the SPWebConfigModification collection). This is where PowerShell comes in.
In this example, I’ll assume the Telerik.Web.UI version that Bamboo products install is 2011.2.915.35 and the version already installed in the farm is 2012.3.1016.35.
Note: We should always redirect all versions found to the newest version of Telerik.Web.UI on the system, for backwards compatibility of functionality.
We need to add the following assembly binding to the Web application web.config to resolve conflict problem:
First, open the SharePoint 2010 Management Shell. From the Start menu, select All Programs > Microsoft SharePoint 2010 Products > SharePoint 2010 Management Shell:
Next, create the SPWebConfigModification object:
Descriptions of the properties:
- Path: A string that contains an XPath expression that specifies the node. If the Type property specifies EnsureChildNode, the Path property contains the XPath expression for the parent node under which the node is created. In this example, the runtime element of the web.config file is placed in another xml-namespace, so we have to identify the namespace. Otherwise, that element will not be found.
- Name: A string that contains the name of the attribute or section node. When the type of web.config modification is EnsureChildNode, the Name property refers to an XPath expression that uniquely identifies the node under the parent node, as identified the Path property.
- Sequence: A 32-bit unsigned integer between 0 and 65536. This property throws an InvalidOperationException if the number is outside this range. A sequence is used only if there is more than one modification of the same type to the same node or attribute. In this case, modifications with lower sequence numbers are applied first.
- Owner: A string that specifies the owner of the modification. You can use the Owner property to track ownership of modifications.
- Type: An SPWebConfigModification.SPWebConfigModificationType value that specifies the type of modification. There are 3 types:
- EnsureChildNode: Specifies that the web.config modification must ensure the existence of a child node of the node to which the XPath expression points. Value = 0.
- EnsureAttribute: Specifies that the web.config modification must ensure that there is a value for a particular attribute. Value = 1.
- EnsureSection: Ensures a singleton node. This node is only written once, even if multiple entities register multiple instances of EnsureSection to ensure the existence of a section. Value = 2.
- Value: If the Type property specifies EnsureChildNode, the Value property contains an XML string that represents the node to be populated as a child of the specified parent node. If the type is EnsureSection, Value contains the name of the section. If the type is EnsureAttribute, Value contains the value set in the attribute.
Then, apply the modification to the Web application (replace ‘WebApplicationURL’ with the actual URL of the Web application):
- Be extra careful when making changes to the web.config. Do not add or remove something unless you are absolutely sure what it is.
- Note that Bamboo products are only tested with the Telerik version that they install. Therefore, if a newer Telerik version is used, there might be some unexpected problem.
- Use PowerShell to Script Changes to the SharePoint Web.config File
- Adding bindingredirects through SPWebConfigModification
- SPWebConfigModification members