I’ve recently run into quite a problem updating the toolbar property on the Microsoft.SharePoint.WebPartPages.ListViewWebPart. It seems as if there should just be a property exposes of the ListViewWebPart called toolbar but there isn’t. I find a number of different articles that describe various methods for changing the property but in the end no one solution completely worked.

I’ll give most of the credit for this aritcle to Tippu.

The problem I had following his example was that the code actually removed the toolbar from the list view but not the list view webpart.

I took his example and used the last part of the code which works, but combined it with a few other examples I found that didn’t completely work either. Hope it helps many more.

private static void ChangeListViewWebPartToolBar(ListViewWebPart lvwp, string toolbarType)
    // Get the SPView object that controls what the web part displays
    PropertyInfo props = lvwp.GetType().GetProperty("View", BindingFlags.NonPublic  BindingFlags.Instance);
    SPView webpartView = props.GetValue(lvwp, null) as SPView;

    // This line is required to ensure that all the appropriate internal nodes of the SPView are populated
    String temp = webpartView.SchemaXml;

    // Get the internal XML for the view so we can edit it
    PropertyInfo nodeProp = webpartView.GetType().GetProperty("Node", BindingFlags.NonPublic  BindingFlags.Instance);
    XmlNode node = nodeProp.GetValue(webpartView, null) as XmlNode;

    // Now get the Toolbar node from the view so we can update its type property
    XmlNode toolbarNode = node.SelectSingleNode("Toolbar");

    if (toolbarNode != null)
        switch (toolbarType)
            case "Standard":

            case "FreeForm":

                toolbarNode.Attributes["Type"].Value = "FreeForm";

                string newItemString = string.Empty;
                XmlAttribute pos = toolbarNode.OwnerDocument.CreateAttribute("Position");
                pos.Value = pos.Value = "After";

                switch (webpartView.ParentList.BaseTemplate)
                    case SPListTemplateType.DiscussionBoard:
                        newItemString = "discussion";
                    case SPListTemplateType.Links:
                        newItemString = "link";
                    case SPListTemplateType.Tasks:
                        newItemString = "task";
                    case SPListTemplateType.DocumentLibrary:
                        newItemString = "document";
                    case SPListTemplateType.Announcements:
                        newItemString = "announcement";
                    case SPListTemplateType.GenericList:
                        newItemString = "item";

                tBarNode.InnerXml = @"<IfHasRights><RightsChoices><RightsGroup ";
				tBarNode.InnerXml += @"PermAddListItems=”"required”" />";
				tBarNode.InnerXml += @"</RightsChoices><Then><HTML><![CDATA[ ";
				tBarNode.InnerXml += @"<table width=100% cellpadding=0 cellspacing=0 border=0 >";
				tBarNode.InnerXml += @"<tr> <td colspan=""2"" class=""ms-partline"">”;";
				tBarNode.InnerXml += @"<IMG src=""/_layouts/images/blank.gif"" width=1 height=1 ";
				tBarNode.InnerXml += @"alt=""""></td> </tr> <tr> <td ";
				tBarNode.InnerXml += @"class=""ms-addnew"" style=""padding-bottom: 3px"">";
				tBarNode.InnerXml += @" <img src=""/_layouts/images/rect.gif"" ";
				tBarNode.InnerXml += @"alt="""">&nbsp;<a class=""ms-addnew"" ID=""idAddNewItem""";
				tBarNode.InnerXml += @"href=""]]></HTML><URL Cmd=”"New”" />";
				tBarNode.InnerXml += @"<HTML><![CDATA["" ONCLICK=""javascript:NewItem(']]>";
				tBarNode.InnerXml += @"</HTML><URL Cmd=”"New”" /><HTML><![CDATA[', ";
				tBarNode.InnerXml += @"truejavascript:return false;"" target=""_self"">]]>";
				tBarNode.InnerXml += @";</HTML><HTML>Add a new ” + NewItemString + @”";
				tBarNode.InnerXml += @"</HTML><HTML><![CDATA[</a>";
				tBarNode.InnerXml += @"</td> </tr> <tr><td><IMG ";
				tBarNode.InnerXml += @"src=""/_layouts/images/blank.gif"" width=1 height=5 ";
				tBarNode.InnerXml += @"alt=""""></td></tr> ";
				tBarNode.InnerXml += @"</table>]]></HTML>";
				tBarNode.InnerXml += @"</Then></IfHasRights>";

            case "None":
                toolbarNode.Attributes["Type"].Value = "None";


Posted by Chris Buchanan

Leave a reply

Your email address will not be published. Required fields are marked *