Set ListViewWebPart Toolbar Type Programmatically

While adding a ListViewWebPart to web part page programmatically, had to set the List View’s tool bar to “Summary View”. After trying for hours found this code:

/Method to Set the Tool bar type
public static void SetToolbarType(SPView spView, String toolBarType)
	spView.GetType().InvokeMember("EnsureFullBlownXmlDocument", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, spView, null, System.Globalization.CultureInfo.CurrentCulture);

	System.Reflection.PropertyInfo nodeProp = spView.GetType().GetProperty("Node", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
	XmlNode node = nodeProp.GetValue(spView, null) as XmlNode;
	XmlNode toolbarNode = node.SelectSingleNode("Toolbar");

	if (toolbarNode != null)
		toolbarNode.Attributes["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 = "After";

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

			if (spView.ParentList.BaseType == SPBaseType.DocumentLibrary)
				newItemString = "document";

			// 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>";

Call the above method wherever required:

//Create the object SPLimitedWebPart Manager
SPLimitedWebPartManager WebPartMgr = web.GetLimitedWebPartManager("Home.aspx", PersonalizationScope.Shared);

//Add Client Notes List view web part
SPList ProjectNotesList = web.Lists["Project Notes"];
ListViewWebPart ProjectNotesListViewWP = new ListViewWebPart();
//Set the properties of the webpart
ProjectNotesListViewWP.ChromeType = PartChromeType.None;
ProjectNotesListViewWP.Title = "Client Notes";
ProjectNotesListViewWP.ListName = ProjectNotesList.ID.ToString("B").ToUpper();
ProjectNotesListViewWP.ViewGuid = ProjectNotesList.DefaultView.ID.ToString("B").ToUpper();
//Define the zone in which webparts need to be added
WebPartMgr.AddWebPart(ProjectNotesListViewWP, "Left", 1);

//Set the Toolbar
System.Reflection.PropertyInfo ProjectNotesViewProp = ProjectNotesListViewWP.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
spView = ProjectNotesViewProp.GetValue(ProjectNotesListViewWP, null) as SPView;
Helper.SetToolbarType(spView, "Freeform");


Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “Set ListViewWebPart Toolbar Type Programmatically

  • In SP2016, it looks like ViewGuid property must be assigned to the web part after adding the web part.

  • Thank you very much. You saved a lot of my time.

  • Thank you. After much searching this one works perfectly.


Leave a Reply

Your email address will not be published.