SharePoint 2010 Cookbook: How to Add a web.config Modification Using PowerShell

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?

Challenge:

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.

Solution:

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):

Discussion:

  • 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.

References:


SharePoint Online

The cloud parts are functional components that extend your SharePoint Online environment in Microsoft 365.

Supports Classic and Modern sites for SharePoint Online/Microsoft 365

SharePoint

Top SharePoint Online Products

Experience greater power and savings by bundling our SharePoint apps and cloud parts.


Calendar Plus


Chart Plus


Knowledge Base


Project Management Central


Simple List Search

 

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2013, 2016, 2019 – Classic Pages Only

SharePoint

Top On-Premises Only Products

Experience greater power and savings by bundling our SharePoint apps and web parts.


Calendar Plus


Data Viewer


Password Change


Password Expiration


Password Reset

 

Featured Services

SharePoint Health Check

A SharePoint Health Check will identify the causes of issues and risks associated with your specific environment, and is custom tailored to provide you with the best recommendations to optimize your SharePoint environment.

SQL Health Check

Document recommendations relating to performance, stability, availability, or a specific focus you request of your SQL Server database instances.

My SharePointXperts

The truth is that each SharePoint skill may not be a full time job for many organizations, and it is nearly impossible for one person to do everything you need – so augment your team with SharePointXperts; providing the skill sets you need when you need them!