Adding/Removing toolbar from custom ListViewWebPart

Refer from:

Last week I got a requirement to develop a web part which will display a SharePoint list’s data together with some other content. And the SharePoint list’s data should be displayed same as displayed in the default SharePoint list view.

So to display list data, I used SharePoint’s out-of-the-box webpart named ListViewWebPart which can be found in the Microsoft.SharePoint.WebPartPages namespace. Refer following code snippet:

public class CustomListViewWebPart : WebPart
{
  protected override void CreateChildControls()
  {
    base.CreateChildControls();
    //Code to add ListViewWebPart
    SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists[“CustomListName”];
    SPView wpView = list.Views[“ViewName”];
    ListViewWebPart _wpListView = new ListViewWebPart();
    _wpListView.ID = "_wpListView";
    _wpListView.ListName = list.ID.ToString("B").ToUpper();
    _wpListView.ViewGuid = wpView.ID.ToString("B").ToUpper();
    _wpListView.GetDesignTimeHtml();
    this.Controls.Add(_wpListView);
    //Code to display some other content
  }
}

The above code snippet was rendering the list data same as default SharePoint list view, but the list toolbar was not rendered properly i.e. some of the menu items were missing and others which rendered, were not working. I tried my level best to make it working, but I didn’t found any solutions.

So finally I decided to hide the toolbar from the ListViewWebPart and add a toolbar manually by using following code snippet:

//Code to add List ToolBar
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[“CustomListName”];
SPView wpView = list.Views[“ViewName”];
ViewToolBar _ViewToolBar = new ViewToolBar();
_ViewToolBar.ID = "_ViewToolBar";
_ViewToolBar.RenderContext = SPContext.GetContext(this.Context, wpView.ID, list.ID, web);
this.Controls.Add(_ViewToolBar);

But again I just hanged with another issue; I was not able to hide toolbar from ListViewWebPart properly. I followed a series of approaches to do it but these were creating some issues in some other places of the website. But finally I found an approach to hide toolbar without any issues.

Here are the all the approaches I have used:

Approach 1 (Worked with one side effect):

SharePoint uses a CSS class “ms-menutoolbar” to apply styles in the list toolbar. So I have just added one more attribute in this class:

.ms-menutoolbar
{
  display: none; //Added by myself
  // All other default attributes
}

With this change, toolbar disappeared from the CustomListViewWebPart, but also from all the SharePoint’s default list views because these are also using the same CSS class.

So this approach was rejected.

Approach 2 (Worked with one side effect):

In this approach I had modified the XmlSchema of SPView object and set the “Type” property of “Toolbar” node to “None”. Here is the code snippet.

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[“CustomListName”];
SPView wpView = list.Views[“ViewName”];
//Code to add List ToolBar
ViewToolBar _ViewToolBar = new ViewToolBar();
_ViewToolBar.ID = "_ViewToolBar";
_ViewToolBar.RenderContext = SPContext.GetContext(this.Context, wpView.ID, list.ID, web);
this.Controls.Add(_ViewToolBar);
//Code to add ListViewWebPart
ListViewWebPart _wpListView = new ListViewWebPart();
_wpListView.ID = "_wpListView";
_wpListView.ListName = list.ID.ToString("B").ToUpper();
_wpListView.ViewGuid = wpView.ID.ToString("B").ToUpper();
//Modify the SPView schema and set the ToolBar property to “None”
String strtemp = webPartView.SchemaXml;
System.Reflection.PropertyInfo ViewProp = _wpListView.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
SPView spTempView = ViewProp.GetValue(_wpListView, null) as SPView;
// This forces a refresh of the views internal xml or the node's cild nodes are not populated
PropertyInfo nodeProp = wpView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;
// Now get the Toolbar node from the view so we can update its type property
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
  toolbarNode.Attributes["Type"].Value = "None";
  web.AllowUnsafeUpdates = true;
  wpView.Update();
  web.AllowUnsafeUpdates = false;
}
_wpListView.GetDesignTimeHtml();
this.Controls.Add(_wpListView);

With this code snippet, toolbar disappeared from the CustomListViewWebPart, but again the toolbar is also disappeared from the list’s selected view i.e. if I will open this list in the SharePoint list view and open the view which I have used here to get an instance of SPView object, the toolbar will not appear because I have called a method wpView.Update() which made this change permanent.

So again this approach was not acceptable.

Approach 3 (Worked without any side effects):

Finally I have used following code snippet which worked successfully without any side effects.

In this approach, just loop through the Controls collection of ListViewWebPart, find the “ViewToolBar” control and set its “Visible” property to “False”. Here is the code snippet. 

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[“CustomListName”];
SPView wpView = list.Views[“ViewName”];
//Code to add List ToolBar
ViewToolBar _ViewToolBar = new ViewToolBar();
_ViewToolBar.ID = "_ViewToolBar";
_ViewToolBar.RenderContext = SPContext.GetContext(this.Context, wpView.ID, list.ID, web);
this.Controls.Add(_ViewToolBar);
//Code to add ListViewWebPart
ListViewWebPart _wpListView = new ListViewWebPart();
_wpListView.ID = "_wpListView";
_wpListView.ListName = list.ID.ToString("B").ToUpper();
_wpListView.ViewGuid = wpView.ID.ToString("B").ToUpper();
//Find the ToolBar control and set visible to False
foreach (Control ctrl in _wpListView.Controls)
{
  if (ctrl.GetType() == typeof(ViewToolBar))
  {
    ctrl.Visible = false;
    break;
  }
}
_wpListView.GetDesignTimeHtml();
this.Controls.Add(_wpListView);

Comments

Popular posts from this blog

Setup SharePoint 2010

Register CSS to SP Master Page