Compartilhar via


IIS7 – Adding your UI extension to the IIS manager hierarchy

In the last post I was talking about writing a simple UI extension which would appear like below:

image

How about you adding this to the IIS manager hierarchy – just below “Application Pools” and “FTP Sites”? Here is what you need to do additional to the steps you followed using my previous blog on this.

  1. Add a new class to the project and name it as MyHierarchyProvider, and the file as MyHierarchyProvider.cs
  2. MyHierarchyProvider should derive from Microsoft.Web.Management.Client.HierarchyProvider
  3. It should have an internal class deriving from Microsoft.Web.Management.Client.HierarchyInfo where you can extend few properties to specify the display text, and which ModulePage it links to.

Below is a sample code which would define a class deriving from HierarchyProvider, and has an internal class with all its properties set.

MyHierarchyProvider.cs

 using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Management.Client;

namespace MyIIS7UIExtensions
{
    internal class MyHierarchyProvider : HierarchyProvider
    {
        public MyHierarchyProvider(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }
        public override HierarchyInfo[] GetChildren(HierarchyInfo item)
        {
            if (item.NodeType == HierarchyInfo.ServerConnection)
            {
                return new HierarchyInfo[] { new DemoHierarchyInfo(this) };
            }

            return null;
        }
        internal class DemoHierarchyInfo : HierarchyInfo
        {

            public DemoHierarchyInfo(IServiceProvider serviceProvider)
                : base(serviceProvider)
            {
            }

            public override string NodeType
            {
                get
                {
                    return "MyHierarchyProvider";
                }
            }

            public override bool SupportsChildren
            {
                get
                {
                    return false;
                }
            }

            public override string Text
            {
                get
                {
                    return "SimpleIIS7UIModule";
                }
            }

            protected override bool OnSelected()
            {
                return Navigate(typeof(MyPage));
            }
        }
    }
}

After doing above, you might also want to make some changes in your existing Module class, so that it has the information about the hierarchy. If you have downloaded the sample which I have linked in my earlier post, you should be having 2 lines commented (I forgot to remove them last time.. lol) – uncomment them, and you are good to go. However below are the 2 lines which you need to add to link this hierarchy provider with our module.

IExtensibilityManager extensibilityManager = (IExtensibilityManager)GetService(typeof(IExtensibilityManager));

extensibilityManager.RegisterExtension(typeof(HierarchyProvider), new MyHierarchyProvider(serviceProvider));

This would register the HierarchyProvider class to our module, and after building our assembly, and placing the DLL in GAC successfully, and after having all the administrationHost.config settings, you must see the below:

image

And, you can change the Text property of our HierachyInfo which is an internal class to change the display name appearing in the UI. Don’t hesitate to put your questions or anything you want to let me know through comments!

Happy learning.