Home > MOSS 2007 > ListViewWebPart: Programmatically setting the ToolbarType property

ListViewWebPart: Programmatically setting the ToolbarType property

While working on a migration project we came across a requirement whereby ListViewWebPart’s had to be added to a page programmatically. The ListViewWebPart class has a property called toolbartype. Setting the toolbartype property sounds simple enough. I mean how difficult can it be to set a property?

The problem is that it is a read only property. We thought maybe we can set it via reflection. After searching a bit on google we found a couple of suggestions on how it could be done (most of them were solutions on how to set it to none). One method seemed to have solved our problem until we installed the latest infrastructure updates. Then it completely stopped working. However luckily for us Tony Stegeman and Brian Farhill came up with a solution.

Below you can see the complete solution that worked for us:

private static void SetToolbarType(SPView spView, string toolBarType)
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
    null, spView, null, System.Globalization.CultureInfo.CurrentCulture);
    PropertyInfo nodeProp = spView.GetType().GetProperty("Node",
    BindingFlags.NonPublic | BindingFlags.Instance);
    XmlNode node = nodeProp.GetValue(spView, null) as XmlNode;
    XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
    if (toolbarNode != null)
"Type"].Value = toolBarType;
        // If the toolbartype is Freeform (i.e. Summary Toolbar) then we need to manually 
        // add some CAML to get it to work.
        if (String.Compare(toolBarType, "Freeform", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
            string newItemString = "";
            XmlAttribute positionNode = toolbarNode.OwnerDocument.CreateAttribute("Position");
            positionNode.Value =
            switch (spView.ParentList.BaseTemplate)
                case SPListTemplateType.Announcements:
                    newItemString =
                case SPListTemplateType.Events:
                    newItemString =
                case SPListTemplateType.Tasks:
                    newItemString =
                case SPListTemplateType.DiscussionBoard:
                    newItemString =
                case SPListTemplateType.Links:
                    newItemString =
                case SPListTemplateType.GenericList:
                    newItemString =
                case SPListTemplateType.DocumentLibrary:
                    newItemString =
                    newItemString =
            if (spView.ParentList.BaseType == SPBaseType.DocumentLibrary)
                newItemString =
            // Add the CAML
            toolbarNode.InnerXml = @"<IfHasRights><RightsChoices><RightsGroup PermAddListItems=""required"" /></RightsChoices><Then><HTML><![CDATA[ <table width=100% cellpadding=0 cellspacing=0 border=0 > <tr> <td colspan=""2"" class=""ms-partline""><IMG src=""/_layouts/images/blank.gif"" width=1 height=1 alt=""""></td> </tr> <tr> <td class=""ms-addnew"" style=""padding-bottom: 3px""> <img src=""/_layouts/images/rect.gif"" alt="""">&nbsp;<a class=""ms-addnew"" ID=""idAddNewItem"" href=""]]></HTML><URL Cmd=""New"" /><HTML><![CDATA["" ONCLICK=""javascript:NewItem(']]></HTML><URL Cmd=""New"" /><HTML><![CDATA[', true);javascript:return false;"" target=""_self"">]]></HTML><HTML>Add new " + newItemString + @"</HTML><HTML><![CDATA[</a> </td> </tr> <tr><td><IMG src=""/_layouts/images/blank.gif"" width=1 height=5 alt=""""></td></tr> </table>]]></HTML></Then></IfHasRights>";

  1. March 8, 2010 at 10:30 am

    Sharepoint customisation is a right pain, is there anyway to avoid using CAML for things like list view rendering?

  2. November 1, 2010 at 9:14 pm

    Thanks allot,
    It worked for me (SP 2007).

    Kind regards.

  3. January 20, 2013 at 1:19 pm

    I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to construct my own blog and would like to find out where u got this from. cheers

  4. March 16, 2013 at 4:51 pm

    An impressive share! I’ve just forwarded this onto a co-worker who has been conducting a little homework on this. And he actually ordered me lunch due to the fact that I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending time to discuss this topic here on your site.

  5. September 11, 2014 at 9:48 am

    We are a group of volunteers and pening a brand new scheme in our community.
    Your website offered us with useful information to work on. You have performed an impressive job
    and ourr whole neighborhood can be thankful
    to you.

  1. April 5, 2009 at 8:09 pm
  2. October 2, 2010 at 12:53 pm
  3. October 15, 2010 at 9:53 pm
  4. July 26, 2015 at 6:33 pm
  5. March 29, 2016 at 5:25 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: