Friday, November 15, 2013

ListViewWebPart change ToolBarType

Changing the ToolBarType isn't allowed by the SharePoint API.

On a specific project i had to change the ToolBarType of a ListViewWebPart. My colleague Florent Vignaux helped me, and he released that function :


Code:
private void SetToolBarType(ListViewWebPart lv, string toolbarType)
 {
     SystemReflectionPropertyInfo ViewProp = lv.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
     SPView spView = ViewProp.GetValue(lv, null) as SPView;
 
     // This forces a refresh of the views internal xml or the node's child nodes are not populated
     string txt = spView.SchemaXml;
 
     SystemReflectionPropertyInfo NodeProp = spView.GetType().GetProperty("Node", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 
     // Again we force a refresh of the views
     txt = spView.Toolbar;
 
     XmlNode node = NodeProp.GetValue(spView, null) as XmlNode;
     XmlNode tBarNode = node.SelectSingleNode("Toolbar");
     if (tBarNode != null)
     {
         tBarNode.Attributes["Type"].Value = toolbarType;
         spView.Update();
     }
 }



The ToolbarType value can be : Standard, Freeform or None.

No comments:

Post a Comment