hits counter
  Create Free Blog | Random Blog »   Report Abuse | Login   

 
jump to navigation

How to Set a ListViewWebPart’s Toolbar Type as Summary Web Part October 8, 2008

Posted by tippu in : Sharepoint , trackback
Tags:

When we are creating a ListViewWebPart through program, Even if we set the View for the ListViewWebPart, Sharepoint displays the web part with full toolbar property set on it. The only option that we can change this view is to set the View for the ListViewWebPart and add the webpart to the Web Part Manager. After adding the web part on the page, load the webpart again and get the view which is cached by the list view web part and update the Type by reading the xml node and updating the type to the desired toolbar type. Once the Toolbar type is set to “Freeform” (i.e Summary Toolbar), we can add the innerXml to the node having the CAML for showing the “add new item” link to the toolbar. Make sure the position attribute in the xml is “After”. This will make the web part to show the “add new item” link below the List View Web Part.

[code]

public static void SetWebPartToolbarAttribute(SPView spView, String AttributeValue)
{
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"];
tBarNode.RemoveAll();
tBarNode.Attributes.Append(typeNode);
typeNode.Value = AttributeValue;
}

if (String.Compare(AttributeValue, "Freeform", true) == 0)
{
string NewItemString = "";
XmlAttribute pos = tBarNode.OwnerDocument.CreateAttribute("Position");
pos.Value = pos.Value = "After";
tBarNode.Attributes.Append(pos);

switch (spView.ParentList.BaseTemplate)
{
case SPListTemplateType.DiscussionBoard:
NewItemString = "discussion";
break;
case SPListTemplateType.Links:
NewItemString = "link";
break;
case SPListTemplateType.Tasks:
NewItemString = "task";
break;
case SPListTemplateType.GenericList:
NewItemString = "item";
break;
case SPListTemplateType.DocumentLibrary:
NewItemString = "document";
break;
}

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

[/code]

Share SocialTwist Tell-a-Friend 

Comments

1.  Frode Magnussen - December 22, 2008

This solved a few issues for me. Works excellent. Thanks a lot.
2.  sushama pradhan - December 30, 2008

How to remove toolbar from listviewwebpart in sharepoint or MOSS ?

sample code
—————–
ListViewWebPart lvwp = new ListViewWebPart();
lvwp.WebId = siteWeb.ID;
lvwp.ListName = listGuid.ToString(“B”).ToUpper();
lvwp.ViewGuid = lvGuid.ToString(“B”).ToUpper();
lvwp.GetDesignTimeHtml();
if (lvwp.Controls.Count > 0)
{
if (lvwp.Controls[0] != null)
{
lvwp.Controls.RemoveAt(0);
}
}

3.  sushama pradhan - December 30, 2008

How to remove toolbar from listviewwebpart in sharepoint or MOSS
sample code?

It is working for me.

ListViewWebPart lvwp = new ListViewWebPart();
lvwp.WebId = siteWeb.ID;
lvwp.ListName = listGuid.ToString(“B”).ToUpper();
lvwp.ViewGuid = lvGuid.ToString(“B”).ToUpper();
lvwp.GetDesignTimeHtml();
if (lvwp.Controls.Count > 0)
{
if (lvwp.Controls[0] != null)
{
lvwp.Controls.RemoveAt(0);
}
}

4.  Harden - March 13, 2009

Good Day. The best of us must sometimes eat our words.
I am from Sweden and now teach English, give please true I wrote the following sentence: “Book very cheap airline tickets to europe with tickets to europe.”

Thanks ;) . Harden.

5.  Govind - January 7, 2010

Hi all,

The below contains the code to hide the toolbar which worked for me.
Hope it saves time for others check this out.

http://www.etechplanet.com/post/2009/02/05/AddingRemoving-toolbar-from-custom-ListViewWebPart.aspx

youtube download
Powered by Blog.co.in and Zorg Directory
Powered By Indic IME