Enabling Blob Cache programmatically

Whenever someone comes with the idea of editing the web.config file, I shiver. We’ve already had some issues where the Microsoft SharePoint Foundation Web Application had to be restarted. SharePoint then deletes all the websites from IIS and recreates them from scratch where, obviously, custom changes aren’t applied.

A solution for this problem is either by using custom web.config xml files in the hive, or programmatically by using SPWebConfigModification. The MSDN article explains a lot.

I only wanted to modify the web.config of one specific web application, so I used the SPWebConfigModification through a feature.

I started out with an empty SharePoint project and added a feature:

add feature

After your feature is added, change the scope to Web Application. You really don’t want your site collection administrators fiddling with your web.config files. Also add an event receiver so we can add code to our project.

add event receiver

When your event receiver is added, the cs file will pop-up in visual studio. If it doesn’t do this simply open the added cs file under your feature file.

The next step is enabling the FeatureActivated and FeatureDeactivating code blocks. Simply uncomment them.

What I wanted to achieve is enabling the BlobCache and put the size to 5GB. I used the following code block for FeatureActivated:

        const string owner = "BLOBCache";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
            SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;

            // Write the modification
            SPWebConfigModification webMod = new SPWebConfigModification();
            webMod.Path = "configuration/SharePoint/BlobCache";
            webMod.Name = "enabled";
            webMod.Value = "true";
            webMod.Sequence = 0;
            webMod.Owner = owner;
            // SPWebConfigModificationType.EnsureChildNode
            // SPWebConfigModificationType.EnsureAttribute
            // SPWebConfigModificationType.EnsureSection
            webMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;

            // Write the modification
            SPWebConfigModification webMod2 = new SPWebConfigModification();
            webMod2.Path = "configuration/SharePoint/BlobCache";
            webMod2.Name = "maxSize";
            webMod2.Value = "5";
            webMod2.Sequence = 0;
            webMod2.Owner = owner;
            webMod2.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;

            // Apply the modification

By using the following code I was able to modify the already existing BlobCache tag to “enabled” and put the size value from “10” to “5”!

It’s very important to keep track of the owner of the web modification. By using this owner we can track all changes made by a specific solution, and if necessary remove them. If we want to reverse our modifications we can use the following code block in FeatureDeactivating:

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            SPWebConfigModification configModFound = null;
            SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
            Collection<SPWebConfigModification> modsCollection = webApplication.WebConfigModifications;

            // Find the most recent modification of a specified owner
            int modsCount1 = modsCollection.Count;
            for (int i = modsCount1 - 1; i > -1; i--)
                if (modsCollection[i].Owner == owner)
                    configModFound = modsCollection[i];

            // Remove it and save the change to the configuration database

            // Reapply all the configuration modifications

Because the owner is a constant it’s easy to keep track the modifications made by the feature.

By making the feature specifically for one web application, we don’t want it to activate by default. Change the property “Activate On Default” to “False”. You can find the property by double clicking on the feature file, then it will show up in the properties window:

activate feature by default

When the solution is deployed to the farm, go to your central administration and click “Manage Web Applications”. Select the web application you want to enable the web.config modifications and click “Manage Features”.

manage web application features

When you click “Activate” next to your feature the web.config modifications should be made and you should see that your feature is active in the web feature dialog:

activate feature on web application

When the web.config file is checked, you’re able to see the modification has been applied!

blobcache enabled true

Now whenever a web front end server is added, or the web application service has to be restarted, the web.config modifications will be automatically applied.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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