How to Set a ListViewWebPart’s Toolbar Type as Summary Web Part October 8, 2008
Posted by tippu in : Sharepoint , trackbackWhen 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=""""> <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]
|
|
|


Comments
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);
}
}
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);
}
}
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.
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