Skip to main content

Mark Stokes

Go Search
Home
People & Blogs
Twitter
Wiki
Events
Forums
Resources
Jobs
Support
Account
SP2010
  

Sharepoint Studio > People & Blogs > Mark Stokes > Posts > Changing (or removing) the Toolbar from a ListViewWebPart
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:
 

SPView myView = list.Views["View Name"];

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:
 

<Toolbar Type="None" />
  or
<Toolbar Type="Freeform" />
  or
<Toolbar Type="Standard" />
  or
<Toolbar Type="Full" />

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

    }
}

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

// Get Webpart View
System.Reflection.PropertyInfo ViewProp = wp.GetType().GetProperty("View", BindingFlags.NonPublic | BindingFlags.Instance);

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

WebParts.SetToolbarType(spView, "None")

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

SPView myIssuesView = list.Views["My Issues"];

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, false, false, 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);

// Get Webpart View
System.Reflection.PropertyInfo ViewProp = wp.GetType().GetProperty("View", BindingFlags.NonPublic | BindingFlags.Instance);

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

SetToolbarType(spView, "None");

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

Comments

Code update

After posting, I noticed a small error in my code why I have updated in the main article.

I as calling SerToolBarType with the view from the list. You have to extract the view from the ListViewWebPart and call SetToolbarType on that view.

// Get Webpart View
System.Reflection.PropertyInfo ViewProp = wp.GetType().GetProperty("View", BindingFlags.NonPublic | BindingFlags.Instance);

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

SetToolbarType(spView, "None")
Mark Stokes at 7/2/2009 10:55 AM