Changing (or removing) the Toolbar from a ListViewWebPart

I have just spent a good couple of days on this problem and have finally come across an answer.
 
I am programmatically adding web parts to a SharePoint page and didn’t want the Toolbar to show up.
 
Due to a bug in the SharePoint API the following code should, but doesn’t, work:
 
 myView = list.Views[

myView.Toolbar = “None”;
  or

myView.Toolbar = 
“Freeform”;
  or
myView.Toolbar = 
“Standard”;
  or
myView.Toolbar = 
“Full”;

myView.Update();

 
Unfortunately, the Toolbar poperity of an SPView returns the InnerXml of the Toolbar node and we need to set an attribute on the parent node:
 
<
 
So, how do we go about setting the toolbar property?
 
There are a lot of discussions out there on t’Internet, with a number of solutions that worked right up until the Infrastructure Updates were released which broke almost all of them.
 
The resources I was reading through and trying were:

There are many more, but these are the ones that led me to my final solution.

In the end I came across this post from Jalil Sear

Original Kudos needs to be sent out to Ton Stegeman and Brian Farhill for this post.

Ok, now that the acknowledgements are out of the way, what was the final solution?

I used the following method from Jalil’s blog (its all his code)

public static void SetToolbarType(SPView spView, String toolBarType)
{
    spView.GetType().InvokeMember(
“EnsureFullBlownXmlDocument”BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.InvokeMethod, null, spView, null, System.Globalization.CultureInfo.CurrentCulture);

    PropertyInfo nodeProp = spView.GetType().GetProperty(“Node”BindingFlags.NonPublic |BindingFlags.Instance);
    
XmlNode node = nodeProp.GetValue(spView, nullas 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();

    }
}

Our original code then just has to call extract the WebPart View and call this method:

SPView myView = list.Views[“View Name”];

For a little more completeness, here is how I add the ListViewWebPart to the page:

SPView myView = list.Views[“View Name”];

StringCollection viewFields = new StringCollection();
viewFields.Add(
“Title”);
viewFields.Add(
“Issue Status”);
viewFields.Add(
“Due Date”);

SPView wpView = list.Views.Add(“Temp View”, viewFields, myIssuesView.Query, 10, falsefalse,SPViewCollection.SPViewType.Html, false);

ListViewWebPart wp = new ListViewWebPart();
wp.ListName = list.ID.ToString(
“B”).ToUpper();
wp.Title = 
“My Issues”;
wp.ViewGuid = wpView.ID.ToString(
“B”).ToUpper();wpm.AddWebPart(wp, “LeftMiddleLeft”, 1);

list.Views.Delete(wpView.ID);

SPView myView = list.Views[“View Name”];

SPView spView = ViewProp.GetValue(wp, nullas SPView;

SetToolbarType(spView, “None”);

 

 

I hope this goes some way to helping others with this issue.