How to remove a toolbar header from a SharePoint web part

*Moved to: http://fluentbytes.com/how-to-remove-a-toolbar-header-from-a-sharepoint-web-part/

Yes you read it correct, I did some SharePoint development last week. And I must say It was quite a challenge. I was just helping out on a project where we created a feature that combines a custom list deployment with the deployment of a custom page. On this page we place web parts that provide a view to the custom list.

Now the challenge I faced was to get the deployment working in such a way that we add web parts to the page not showing the toolbar. At first I just laugh and said that I would fix that for them in a few minutes, assuming I could just use the object model, set a toolbar property to false and from that point on have a big hug from my colleague.

Well I was wrong. Removing a toolbar from a webpart using the object model is something that is just not supported. You can read more about this in e.g. this forum thread.

After some digging around I finally got the problem solved. I thought I would provide the code snippet here, so you can leverage it.

 

private static void DisableToolbar(ListViewWebPart lv)

{

 //  Extract view

 

   System.Reflection.PropertyInfo ViewProp = lv.GetType().GetProperty(“View”,
    System.Reflection.
BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

 

   SPView spView = ViewProp.GetValue(lv, null) as SPView;

 

   string txt = spView.SchemaXml;

   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 tBarNode = node.SelectSingleNode(“Toolbar”);

 

   if (tBarNode != null)

   {

      XmlAttribute typeNode = tBarNode.Attributes[“Type”];

      // make the contents empty so we realy remove the toolbar …..

      // otherwise you might get a different type of toolbar popup when we have a
      // Migrated site from 2.0

      tBarNode.RemoveAll();

      // re-add the type attribute

      tBarNode.Attributes.Append(typeNode);

      // finally set the toolbar to not show….

       typeNode.Value = “None”;

    }

//This forces a refresh of the views internal xml or the node’s cild nodes are not populated

 spView.Update();
}

Follow my new blog on http://fluentbytes.com