Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Changing the toolpart of a ListViewWebPart after the infrastructure update
Changing the toolpart of a ListViewWebPart after the infrastructure update

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.

Comments

Brian Farnhill

Hey Ton, thanks for providing an update for my sample. I linked to this post from the original one on my blog.
at 12/17/2008 4:11 PM

J Sear

Hi Ton,

Thanks for that it works like a treat. This issue had been bugging us for months!

After fixing this, however, I bumped into another issue. I have just realised that after installing the latest infrasctructure updates we no longer are able to set the view on a listviewwebpart programmatically!

We were able to set this before by setting the guid of the view on the listviewwebpart.viewguid property.
at 1/5/2009 5:09 AM

Point to note re. setting the view before doing the update

Hi Ton, many thanks for this article, it was very useful - a real life saver.
I just wanted to post this in case other people do what i did and cause this code not to work!
I was setting the 'viewGuid' property of the webpart before calling your code which i assumed was being overwritten when i did the subsequent updates. This was not the case and this caused this to not be applied:
                                ListViewWebPart wp5 = new ListViewWebPart();
                                wp5.ViewGuid = list5.DefaultView.ID.ToString("B").ToUpper(); //do not do this!

Another point to note is that as your code removes the toolbar from the listview itself, when you browse to the list e.g. allitems.aspx, the toolbar is missing! This is not good but easily remedied.
Simply re-add the toolbar to the view after doing the update to the webpartmanager like so:
                if (toolbarNode != null)
                {
                    XmlAttribute typeNode = toolbarNode.Attributes["Type"];
                    toolbarNode.RemoveAll();
                    toolbarNode.Attributes.Append(typeNode);
                    typeNode.Value = "None";

                    webpartView.Update();
                    mgr.AddWebPart(currWebPart, "Right", 1);

                    toolbarNode.Attributes.Append(typeNode);
                    typeNode.Value = "Standard";
                    webpartView.Update();

many thanks again,
Ciaran
at 1/12/2009 5:04 AM

Retrieving the ListView web part's View

Retrieving the listview web part's SPView as in the above code does not always work - it didn't in my case.
This is because the listview web part View is not necessarily one of the SPView's defined on the List, since it is merely a copy of the List view (which might not be in sync).

I found that using this to retrieve the webpartView is safer:
PropertyInfo ViewProp = listviewWebPart.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
SPView webpartView = ViewProp.GetValue(lvWebPart, null) as SPView;

cheers
Pascal
at 10/20/2009 8:59 AM

Check this out no need to temper the xml and use Caml

This helped me to solve the toolbar issue.
Hope it helps some one to save time.

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

Thanks
at 1/6/2010 7:08 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).