Tuesday, November 10, 2009

Quick Site Settings: Adding Sub Menus to Site Actions in SharePoint

Tags: programming, sharepoint, wss Categories: Freeware

In this post I'll illustrate how to programmatically add sub menus to the Site Actions menu. This post has an accompanying Visual Studio 2008 solution containing all source code for the Quick Site Settings Feature as discussed below. The source code can be downloaded from the link at the bottom of this post.

The Quick Site Settings Feature adds much of the functionality found on the Site Settings page in WSS v3 to the Site Actions menu for quick access. Here's a screen shot:

I'll keep the code walkthrough at a high level, assuming you have at least intermediate experience in developing solutions for WSS v3. You can download the Visual Studio 2008 solution below to take a closer look, and please feel free to leave comments on this post if you have any questions about how something works. Let's start by taking a look at the Feature declaration. This Feature definition is boilerplate, so I won't discuss it other that to point out the reference to ElementManifest Elements.xml (line 11):

Listing 1

Next is the Elements.xml listing. I would draw your attention to the ControlAssembly and ControlClass attributes of the CustomAction element. These attributes are where we declare the .NET assembly and class which contain the code-behind for our sub menus and sub menu items. Note also that we are declaring the menu items to be located in the SiteActionsSubMenuCustomizer class (line 8). The referenced assembly SiteActionsSubMenuDemo.dll (line 7) should be located in the Global Assembly Cache (GAC) and registered as a SafeControl in SharePoint's web.config:

Listing 2

Finally, Listing 3 is the code for the SiteActionsSubMenuCustomizer class. This class derives from System.Web.UI.WebControls.WebControl and overrides the CreateChildControls (line 21) and OnLoad (line 15) methods of its base class. Aside from a lot of repetitive code for each menu item we're defining, most noteworthy are the use of the SubMenuTemplate and MenuItemTemplate objects. As one might expect, SubMenuTemplate objects (e.g. line 28) can be nested to create a mulitlayered sub menu hierarchy, with MenuItemTemplate objects (e.g. line 41) used to create the individual menu items. Although I'm creating the sub menu structure imperatively in this class, it could also be done declaratively through an XML configuration file. I'll leave the declarative approach as discussion for a future article.

Listing 3


Download SiteActionsSubMenuDemo.zip

DISCLAIMER: My free software is not guaranteed or supported in any way. All software should be evaluated in a test environment before production use. By downloading my free software, you are implicitly agreeing to these terms and conditions.

0 comments: