If you have custom code that changes the toolpart of a listview webpart, you should definitly test this code after installing the Infrastructure Update. I was using this code snippet provided by Brian Farnhill. This worked fine, until we upgraded to the Infrastructure Update. After that, the toolpart settings of our web parts were no longer set. So the disclaimer that Brian gave (this is not supported, and your code can break in the next update) turned out to be the truth. In the code snippet you find the new version. This works if you have the infrastructure update installed. But the same disclaimer still applies. Officially it is not supported, and it can break again in the next update of service pack.
The main change is to call the private void “EnsureFullBlownXmlDocument” using reflection. This ensures that all XML objects are properly initialized, before the Node object is read. If you do not call EnsureFullBlownXmlDocument, the Node property will be null.
using (SPSite site = new SPSite("http://urltoyoursite")) { using (SPWeb web = site.OpenWeb()) { using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)) { ListViewWebPart listWebPart = null; foreach (WebPart wp in manager.WebParts) { if (wp.Title == "Shared Documents") { listWebPart = wp as ListViewWebPart; } } if (listWebPart != null) { SPList list = web.Lists[new Guid(listWebPart.ListName)]; SPView webpartView = list.Views[new Guid(listWebPart.ViewGuid)]; webpartView.GetType().InvokeMember("EnsureFullBlownXmlDocument", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, webpartView, null); PropertyInfo nodeProp = webpartView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance); XmlNode node = nodeProp.GetValue(webpartView, null) as XmlNode; XmlNode toolbarNode = node.SelectSingleNode("Toolbar"); if (toolbarNode != null) { toolbarNode.Attributes["Type"].Value = "None"; webpartView.Update(); } } } } }
In this sample I iterate through all web parts on the page until I find a web part called “Shared Documents”. How you get a reference to your web part is up to you. This is just sample code to quickly find a listview webpart.