SharePoint ToolbarButtons in lists

It has been a while since my last post. My new assignment and a new "spare-time project" have been consuming a lot of time.
Recently I am working on a new assignment, developing all kinds of Web Parts and Features and since I'm fairly new to SharePoint I ran into some nice problems.
One of those issues really had me going bananas and although there is a wide SharePoint community out there, I had difficulties finding the solution. So I figured I'd share my findings with you guys.

The problem

Ok, say we have a document library and I want to put an icon in the toolbar of the document library, so I tried to do this with the default custom action locations; but that only allows me to put it inside a menu. Finally I found the solution to this (thanks to Paul Robinson) by adding a ToolBarButton to the ToolBar of the list. Now as you'll notice there's no constructor for these two classes in the Microsoft.SharePoint.WebControls namespace. To get toolbars in your web part, you need to create a control using the Page.LoadControl method, pointing to the relevant user control and casting the results. If you know which controls to use it's simple:First let's have a look at the elements.xml

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="">
  <CustomAction Id="TestFeature.AddToolBarButtonToList"
    ControlAssembly="TestFeature, Version=, Culture=neutral, PublicKeyToken=9fdd626cc17fc71e"
    Title="New ToolBarButton"
    Description="Use this feature to add a new ToolBarButton to your list menu"

So we register the feature in the list's standardmenu, i used the group newMenu, but it doesn't matter because we need the toolbar. Notice the ControlClass, this is where we define our class which is going to create the ToolBarButton.
In the ControlClass, the CreatChildControls method will be called when the document library is loading:

protected override void CreateChildControls()
 ToolBar listviewToolBar = GetParentToolBar();
 if (listviewToolBar != null)
   // Ok, we have the ToolBar of the listview. Now we can add an extra menu...
   _statusButton = (ToolBarButton)Page.LoadControl("~/_controltemplates/ToolBarButton.ascx");
   _statusButton.Text = "";// Only need the icon, so this left blank intentionally
   _statusButton.ImageUrl = "/_layouts/images/PermissionStatus/authenticated.png";
   _statusButton.Click += new EventHandler(_statusButton_Click);

I have used the .RightButtons to add the icon I need to the most right.

Here's the helper method to get the ToolBar:

private ToolBar GetParentToolBar()
            Control parent = Parent;
            while (parent != null)
                if (parent is ToolBar)
                    return (ToolBar)parent;
                parent = parent.Parent;
            return null;

And finally, handling the click, (which is not yet implemented)

void _statusButton_Click(object sender, EventArgs e)
            throw new NotImplementedException();

And here's the result :


Post a Comment