Skip to content

The Chris Kent

Quick Fixes, Workarounds, and Other Neat Tricks

  • Home
  • About
  • Speaking
HomePosts tagged 'MetaData Navigation'

MetaData Navigation

Changing the Width of the Left Panel

May 11, 2012May 9, 2012 theChrisKent Branding, List/Library Settings, SharePoint CSS, Left Panel, Master Page, MetaData Navigation, MSO_ContentTable, s4-leftpanel, SharePoint, Tree
Applies To: SharePoint 2010

We’ve been using the MetaData Navigation on a site with custom branding and everything’s been working great. Recently, they decided to remove the Key Filters. This was simple enough using the List Settings but I quickly found that the width of the Left Panel automatically cut off my taxonomy item names without the Key Filters there to stretch it.

Users can easily grab the little bar and grow this part of the left panel’s treeview, but why should they have too for initial viewing? Some quick digging in the rendered HTML showed that the Left Panel’s width was set to 155 px by default. All I wanted was to change that to 200 px.

So, I opened my custom master page (based on v4.master) and went to the div with id=”s4-leftpanel” and found that the style was not set there and was being set in code somewhere. No worries, if you do set it here, it won’t be overridden by whatever part of the code assigns these values. So to adjust your default width just add a style attribute to your s4-leftpanel div:

<div id="s4-leftpanel" style="width:200px;">

If you save this and check it on a live site you’ll find things don’t look right. The key is to also adjust the  MSO_ContentTable div to have a matching left margin:

<div id="MSO_ContentTable" style="margin-left:200px;">

Voila! That’s it! The width can still be adjusted by the end user using the grab bar, but the default is set and you are happy.

Rate this:

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • Tumblr
  • Pinterest
  • Reddit
  • LinkedIn

Like this:

Like Loading...
3 Comments

Changing a UserControl in the 14 Hive Using a Custom Timer Job

May 10, 2012May 9, 2012 theChrisKent .NET, List/Library Settings, MetaDataNavExpansion, SharePoint Managed Metadata, MetaData Navigation, MetaDataNavExpansion, MetaDataNavTree, SharePoint, SPServiceJobDefinition, Timer Job, User Control, WireBear
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.

Rate this:

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • Tumblr
  • Pinterest
  • Reddit
  • LinkedIn

Like this:

Like Loading...
Leave a comment

Changing the Default Expansion of MetaData Navigation on Initial Page Load

May 9, 2012 theChrisKent .NET, Information Management, List/Library Settings, MetaDataNavExpansion, SharePoint .NET, Managed Metadata, MetaData Navigation, MetaDataNavTree, SharePoint, Taxonomy, Timer Job, Tree, WireBear
Applies To: SharePoint 2010

We’ve recently begun making fairly heavy use of the MetaData Navigation available for lists. It’s intuitive and easy to use and our users are really liking it. But one minor, but frequently mentioned, irritation brought up by nearly everyone who tried to use the site was that the navigation elements were collapsed by default when they went to the site.

This isn’t a big deal for most people since they get used to where the navigation elements are (just expand the folder), but does create an extra barrier for new users as they try and figure out how to use the site. Microsoft has taken an awesome feature and traded it’s potentially intuitive use to account for some potential performance issues.

The issue seems to be that navigation using taxonomies with several top level items would significantly delay initial page load. This is certainly true, but the solution is not to cripple all uses of MetaData Navigation, the solution is to not use it with those types of taxonomies! Not only would you have had that performance issue on initial expansion anyway, having that many top level items makes for bad navigation. If your taxonomy has several top level items (2000+) then it is either not a good candidate for MetaData navigation or you need to group those items into sub-nodes.

In searching for an answer to this problem I came across this answer on technet by Entan Ming. In it he gives the manual steps to take care of this issue. Here is a brief summary of the steps that must be performed manually on every server:

  1. Open the MetadataNavTree.ascx file in your 14 Hive (TEMPLATE\CONTROLTEMPLATES) using notepad
  2. Change the line ExpandDepth=”0″ to ExpandDepth=”2″
  3. Save the changes and refresh the page(s) with MetaData Navigation

These are easy to do and you can follow them and be done. However, just like PDF Icon Mapping there are some problems with this approach:

  • Manual changes can often be error-prone
  • The change must be performed on every server
  • The change must be performed whenever a new server is added to the farm
  • The change will have to be redone in the event of disaster recovery

So, using the same technique I use for PDF Icon Mapping entries, I’ve created a SharePoint solution to do this automatically.

You can find this solution over on CodePlex as WireBear MetaDataNavExpansion. It is free for personal and commercial use (License here). You can also find basic installation instructions as well. It’s super easy to setup since it’s just a standard SharePoint Solution that you globally deploy.

The full source code is available on CodePlex and I’ll write up another article about what’s really happening, but here’s a general summary:

  • On Activation and Deactivation a one time Service Timer Job is run.
  • On Activation, the Timer Job creates a backup of the MetaDataNavTree.ascx file within the 14\TEMPLATE\CONTROLTEMPLATES folder.
  • The MetaDataNavTree has it’s tree’s ExpandDepth property changed from 0 to 2
  • When Deactivating, the Timer Job restores the original MetaDataNavTree.ascx file

It’s pretty straightforward. It just automates the manual steps listed above. This allows it to be applied automatically to any new servers added to the farm and will be reapplied in the event of disaster recovery.

Here’s what it looks like on initial page load

Standard: With MetaDataNavExpansion:
   
“Der… What do I do?” “WOWEE! This site is amazing!”

Rate this:

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • Tumblr
  • Pinterest
  • Reddit
  • LinkedIn

Like this:

Like Loading...
3 Comments

Office Development

Recent Posts

  • Thank you Microsoft 365 Conference!
  • Getting the Binary Value of an ASCII Character in Power Apps
  • Converting Integers to Binary in Power Apps
  • Converting Binary to Integers in Power Apps
  • Thank you M365 Collaboration Conference!

Popular Posts

  • Applying Column Formats to Multi-line Text Fields
  • Custom Icon Buttons in Power Apps with Hover Color
  • Generate List Formatting Elements in a Loop Using forEach
  • Formatting Values Using "Contains" in List Formatting
  • Validate Email Address Columns in SharePoint

Archives

  • December 2022
  • May 2022
  • April 2022
  • May 2021
  • June 2020
  • April 2020
  • November 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • August 2018
  • July 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • August 2015
  • April 2015
  • February 2015
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
Create a website or blog at WordPress.com
  • LinkedIn
  • Twitter
  • Follow Following
    • The Chris Kent
    • Join 367 other followers
    • Already have a WordPress.com account? Log in now.
    • The Chris Kent
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    %d bloggers like this: