Modifying the SharePoint web.config

So, I recently purchased a System Messages solution that allows me to post global alerts to all sites in the SharePoint farm. I looked at several ways to do this and figured out how to do it using JavaScript but determined that it would take me more time than the $20 price tag to purchase this ability.

This solution works very well for us to notify users of upcoming maintenance windows or other possible disruptions. Additionally, I think I’ll use this to advertise our Campus SharePoint User Group meetings.

Two things came up in applying this solution. First, I had to find a way to activate the feature on all webs. Of course, I did this using PowerShell and I’ll probably write that one up in a separate blog post. Second, after receiving the information from the purchase I figured out that I had to modify the web.config file on all of my WFE in order to register the product. Not ideal, as we have 3 WFEs and 5 Web Apps, which means 15 web.configs. Additionally, this adds complexity if we ever want to add another WFE or Web App…

PowerShell cam to the rescue once more. After a bit of searching on modifying the web.config via powershell lead me to the following Hey Scripting Guy blog post:

This was close to what I needed, but rather than modifying an entry that already exists, I needed to add a new attribute in a specific location. The attribute needed to look something like this:

<add key="LicenseKey" value="MySecretKey" />

and it needed to be added to the <appSettings> section of the web.config file. So, I needed to determine the changes to be made to this excerpt form the blog post mentioned above:

$configMod1.Path = "configuration/SharePoint/BlobCache"
$configMod1.Name = "enabled"
$configMod1.Value = "true"
$configMod1.Sequence = 0
$configMod1.Owner = "BlobCacheMod"
## SPWebConfigModificationType.EnsureChildNode -> 0
## SPWebConfigModificationType.EnsureAttribute -> 1
## SPWebConfigModificationType.EnsureSection -> 2
$configMod1.Type = 1

To help figure out the needed modifications I came across the tool: SPWebConfigModification which allows you to walk through the changes to the web.config without actually modifying it. Also, provides you with the c# code to implement the planed changes which was helpful, but not quite the PowerShell I needed. It was easy enough however to determine how to modify the snippet above to implement my desired changes.

The big difference for me was to change the SPWebConfigModificationType to EnsureChildNode as I was adding a node to the appSettings instead of modifying an attribute. The result was this entire script:

function Enable-SystemMessagesMod {
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]

    process {

        $WebApp = $WebApplication.Read()

        $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
        $configMod1.Path = "configuration/appSettings"
        $configMod1.Name = "add[@key=’SystemMessages_LicenseKey’]"
        $configMod1.Value = "<add key=’LicenseKey’ value=’MySecretKey‘/>"
        $configMod1.Sequence = 0
        $configMod1.Owner = "[SystemMessagesMod]"
        ## SPWebConfigModificationType.EnsureChildNode -> 0
        ## SPWebConfigModificationType.EnsureAttribute -> 1
        ## SPWebConfigModificationType.EnsureSection -> 2
        $configMod1.Type = 0

        # Add mods, update, and apply
        $WebApp.WebConfigModifications.Add( $configMod1 )

Enable-SystemMessagesMod https://mywebappurl

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: