Tuesday, April 10, 2012

Changing Toolbars For List Web Parts

The problem:

I need to always show the toolbar, MOSS 2007-style, on some of the list web parts on the site I am automatically provisioning. Unfortunately, there is no straightforward way to do it via the SharePoint API, for example through the SPWebPart or the SPView objects.

The solution:

I am not sure why Microsoft chose to omit this functionality from the object model, as this seems to be a pretty common requirement. But, it is possible to change the toolbar type if you use reflection. The code below does the trick. Note that I am passing "ShowToolbar" as a parameter to the "SetToolbarType" method.