Applies To: SharePoint 2010, .NET Framework (C#, VB.NET)
As mentioned in a previous post, I’ve recently put together a solution for automatically updating the MetaDataNavTree.ascx User Control to default the MetaData Navigation expansion to include the actual taxonomy items. You can download the solution as well as the source for free from CodePlex here: WireBear MetaDataNavExpansion.
This post is very similar to my post Updating an XML File in the 14 Hive Using a Custom Timer Job and assumes you know some basics about custom timer job creation (If not, check my other post Implementing a Custom SharePoint Timer Job). Either way, most of the code will be given right here anyway.
The goal for this timer job is to either backup the MetaDataNavTree.ascx file in the 14\TEMPLATE\CONTROLTEMPLATES folder with the SharePoint Hive and to adjust the Tree’s ExpandDepth property from 0 to 2 or to restore the backup previously made. Here is the entire MetaDataNavExpansionJob class:
Imports Microsoft.SharePoint.Administration Imports Microsoft.SharePoint.Utilities Imports System.Text.RegularExpressions Public Class MetaDataNavExpansionJob Inherits SPServiceJobDefinition #Region "Properties" Private _userControlPath As String Public ReadOnly Property UserControlPath() As String Get If String.IsNullOrEmpty(_userControlPath) Then _userControlPath = SPUtility.GetGenericSetupPath("TEMPLATE\CONTROLTEMPLATES\MetadataNavTree.ascx") Return _userControlPath End Get End Property Private _userControlBackupPath As String Public ReadOnly Property UserControlBackupPath() As String Get If String.IsNullOrEmpty(_userControlBackupPath) Then _userControlBackupPath = SPUtility.GetGenericSetupPath("TEMPLATE\CONTROLTEMPLATES\MetadataNavTree.ascx.bak") Return _userControlBackupPath End Get End Property Private Const InstallingKey As String = "DocIconJob_InstallingKey" Private Property _installing() As Boolean Get If Properties.ContainsKey(InstallingKey) Then Return Convert.ToBoolean(Properties(InstallingKey)) Else Return True End If End Get Set(ByVal value As Boolean) If Properties.ContainsKey(InstallingKey) Then Properties(InstallingKey) = value.ToString Else Properties.Add(InstallingKey, value.ToString) End If End Set End Property #End Region Public Sub New() MyBase.New() End Sub Public Sub New(JobName As String, service As SPService, Installing As Boolean) MyBase.New(JobName, service) _installing = Installing End Sub Public Overrides Sub Execute(jobState As Microsoft.SharePoint.Administration.SPJobState) AdjustMetaDataNavExpansion() End Sub Private Sub AdjustMetaDataNavExpansion() If _installing Then If My.Computer.FileSystem.FileExists(UserControlPath) Then 'Backup the original My.Computer.FileSystem.CopyFile(UserControlPath, UserControlBackupPath, True) Dim contents As String = My.Computer.FileSystem.ReadAllText(UserControlPath) 'Replace the Expansion with First Level Expansion My.Computer.FileSystem.WriteAllText(UserControlPath, Regex.Replace(contents, "ExpandDepth=""\d+""", "ExpandDepth=""2"""), False) End If Else If My.Computer.FileSystem.FileExists(UserControlBackupPath) Then 'Restore the original My.Computer.FileSystem.MoveFile(UserControlBackupPath, UserControlPath, True) End If End If End Sub End Class
Lines 8-44 are just the declaration of and logic needed to persist some properties. Again, more information can be found in my previous post, but basically I am using the SPJobDefinition’s Properties HashTable to store my own properties as specified in the constructor. Except for in the case of the UserControlPath and UserControlBackupPath properties which are really just wrapping up some logic to get a reference to specific files in the 14 Hive’s TEMPLATE\CONTROLTEMPLATES directory using the SPUtility class.
The Execute method beginning in line 55 is what is called when the Timer Job actually runs. I override this method to ensure my custom code gets called instead. My custom code really begins in the AdjustMetaDataNavExpansion method starting at line 59.
If this job is installing (Running on Solution Activation), the MetaDataNavTree.ascx file is copied to MetaDataNavTree.ascx.bak as a backup of the original in line 63. The UserControl file is then read in as text and a regular expression searches for and replaces the ExpandDepth=”SomeNumber” property and replaces it with ExpandDepth=”2″. This is all done and saved back into the file in lines 66-67.
If this job is uninstalling (Running on Solution Deactivation), the backup file (MetaDataNavTree.ascx.bak) created on activation is restored in line 72.
To run this from activation and deactivation I simply copy my design from the PDFdocIcon project and create and run a new version of the job. Since this code is nearly identical to what I’ve already explained, I won’t go into detail but I will save you some time and have copied it below. This is the Main.EventReceiver:
Option Explicit On Option Strict On Imports System Imports System.Runtime.InteropServices Imports System.Security.Permissions Imports Microsoft.SharePoint Imports Microsoft.SharePoint.Security Imports Microsoft.SharePoint.Administration ''' <summary> ''' This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade. ''' </summary> ''' <remarks> ''' The GUID attached to this class may be used during packaging and should not be modified. ''' </remarks> <GuidAttribute("a885d247-f5e8-4456-abd2-6cfebb2bdfde")> _ Public Class MainEventReceiver Inherits SPFeatureReceiver Public Sub RunMetaDataNavExpansionJob(Installing As Boolean, properties As SPFeatureReceiverProperties) Dim JobName As String = "MetaDataNavExpansionJob" 'Ensure job doesn't already exist (delete if it does) Dim query = From job As SPJobDefinition In properties.Definition.Farm.TimerService.JobDefinitions Where job.Name.Equals(JobName) Select job Dim myJobDefinition As SPJobDefinition = query.FirstOrDefault() If myJobDefinition IsNot Nothing Then myJobDefinition.Delete() Dim myJob As New MetaDataNavExpansionJob(JobName, SPFarm.Local.TimerService, Installing) 'Get that job going! myJob.Title = String.Format("Configuring MetaData Navigation for {0} Expansion", IIf(Installing, "First Level", "Default")) myJob.Update() myJob.RunNow() End Sub Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties) RunMetaDataNavExpansionJob(True, properties) End Sub Public Overrides Sub FeatureDeactivating(ByVal properties As SPFeatureReceiverProperties) RunMetaDataNavExpansionJob(False, properties) End Sub End Class
I hope you’re beginning to see that automating any manual changes to the 14 Hive can follow the Service Timer Job Solution pattern I’ve now demonstrated twice. This isn’t true for everything (Web.config changes should be done through the object model or a config.something.xml file, workflow actions can have their own file, etc.), but for those little one off things that don’t have a better alternative, this is a great way to take care of it.