blog community

Welcome to blog community Sign in | Join | Help
in Search

Marcel de Vries, MVP Team System

.NET Technologies, Architecture and Web Development

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();
}

Published Thursday, April 10, 2008 7:46 PM by marcelv
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Mirrored Blogs said:

Mashup Virtual Earth und SharePoint Wunder der Welt Suche Search Custom Security Trimmer for Novell Netware

April 15, 2008 10:05 AM
 

Robin said:

Hi Marcel,

thanks for posting and figuring this one out ;) Saved my day again!

July 23, 2008 5:02 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems