Using BaseXsltListWebPart and XslListViewWebPart
Working with the . BaseXsltListWebPart sucks, especially when you are trying to use it as a concrete class, extending / changing the way it functions. Everything is sealed or internal, etc. etc. Shock me, shock me with SharePoint. Even if you instantiate an XslListViewWebPart within another WebPart derived class, there are a lot of features that are not available. Most notably, you will lose a bunch of menus.
The best way of doing it is to have a pre-existing BaseXsltListWebPart that already exists. Then you can just loop through the WebParts by using the WebPartManager.WebParts collection, and get a particular WebPart type by using Enumerable.OfType to filter the elements of an IEnumerable based on a specified type, in this case XsltListViewWebPart. Following, we are going to use the XsltListViewWebPart.XmlDefinition property so we can instantiate a new XmlDocument object. After you can execute any changes, then reset the property of the WebPart.
- var view = manager.WebParts.OfType<XsltListViewWebPart>().FirstOrDefault();
- var xmlDoc = new XmlDocument();
- if (view != null) xmlDoc.LoadXml(view.XmlDefinition);
- if (xmlDoc.DocumentElement != null)
- // Do Stuff
- string xmlDefinition = xmlDoc.OuterXml;
- if (view != null) view.XmlDefinition = xmlDefinition;