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";
			toolbarNode.Attributes.Append(positionNode);

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

			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>";
		}
		spView.Update();
	}
}

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

WebPartMgr.SaveChanges(ProjectNotesListViewWP);

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “Set ListViewWebPart Toolbar Type Programmatically

  • October 7, 2017 at 7:29 AM

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

    Reply
  • November 6, 2013 at 1:46 PM

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

    Reply
  • September 7, 2012 at 4:50 PM

    Thank you. After much searching this one works perfectly.

    Reply

Leave a Reply