none
Converting ListViewWebPart to XSLTListViewWebPart on Sharepoint 2010

    Question

  • Hi,

    After upgrading moss 2007 to 2010 on some web sites I've problem with ListViewWebPart(columns are not populated or rows wrong highlighted on mouse over)... I've found 2 possible solutions:

    - Remember settings, remove add old webpart, ad new and configure

    - Switch page to edit mode and open webpart toolpart and click OK/Apply(ListViewWebPart will be converted to XsltListViewWebPart)

    I've to automatize second option... but I don't want to "rewrite" method ApplyChanges() from BaseListWebpart.

    Has anyone encountered this problem after upgrade? Maybe there is third option?

    Regards

    igby



    igby
    Wednesday, February 16, 2011 9:34 PM

Answers

  • Hi,

     

    The ListViewWebPart class is the default list view type in SharePoint 2007 but it is replaced by XsltListVIewWebPart class in SharePoint 2010.

    So, the conversion mechanism is only provided in SharePoint but there is no any API for us to do it as I know.

     

    Regards,

    Seven

    • Marked as answer by Seven M Friday, February 25, 2011 2:05 AM
    Tuesday, February 22, 2011 7:22 AM

All replies

  • Hi,

     

    The ListViewWebPart class is the default list view type in SharePoint 2007 but it is replaced by XsltListVIewWebPart class in SharePoint 2010.

    So, the conversion mechanism is only provided in SharePoint but there is no any API for us to do it as I know.

     

    Regards,

    Seven

    • Marked as answer by Seven M Friday, February 25, 2011 2:05 AM
    Tuesday, February 22, 2011 7:22 AM
  • Hi,

    We are experimenting the same issue and the first solution of igby84 is working fine

    But because we have tons of libraries in this situation, we can't go manually through all of them.

    I have tryed to save the WebPart programaticaly but it is not converting the view to a xsltListViewWebPart...

    Any news about this issue?

    Wednesday, January 11, 2012 9:11 PM
  • Hi Edouard,

    I fixed this issue by creating some (ugly) code which is doing same things as "Apply/Ok" buttons in web part. I didn't found any better solution...

     

    BR

    Thursday, January 12, 2012 2:16 PM
  • I tryed to do that too (saving the WP in the code) but it is not translating the ListView to an XSLTlistView

    I'm about to create a XSLTlistview, copy all the property of the ListView, add the XSLT in the SPLimitedWebPartManager of the page and delete the old one...

    Thursday, January 12, 2012 2:22 PM
  • You can try to create some crawler which will open each webpart and click edit.

    I was using something like this for updating web parts

    using (SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(relativUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
    {
    	
    	foreach (WebPart webPart in mgr.WebParts)
    	{
    
    	
    		if (webPart.GetType() == typeof(ListViewWebPart))
    		{
    			PropertyInfo viewPropertyInfo = webPart.GetType().GetProperty("View",
    																		  BindingFlags.NonPublic |
    																		  BindingFlags.Instance |
    																		  BindingFlags.GetProperty);
    			
    			var uncustomizedViewByBaseViewId = (SPView)viewPropertyInfo.GetValue(webPart, null);
    
    			if (((uncustomizedViewByBaseViewId != null) && (uncustomizedViewByBaseViewId.Type == "GRID")))
    			{
    				//Skipping webpart, view type is GRID
    				continue;
    			}
    
    			Guid webPartTypeId;
    			if ((uncustomizedViewByBaseViewId != null) && (uncustomizedViewByBaseViewId.Type != "HTML"))
    			{
    				webPartTypeId = ListWebPartTypeId;
    			}
    			else if (uncustomizedViewByBaseViewId == null)
    			{
    				webPartTypeId = XslListWebPartTypeId;
    			}
    			else
    			{								
    				PropertyInfo listInfo = webPart.GetType().GetProperty("List",
    																	  BindingFlags.NonPublic |
    																	  BindingFlags.Instance);
    			
    				var list = (SPList)listInfo.GetValue(webPart, null);
    
    				MethodInfo method = typeof(SPWebPartManager).GetMethod("UseDataView",
    																		BindingFlags.NonPublic |
    																		BindingFlags.Static |
    																		BindingFlags.InvokeMethod, null,
    																		new[]
    																			{
    																				typeof (SPList),
    																				typeof (SPView)
    																			}, null);
    				
    				var b = (bool)method.Invoke(null, new object[] { list, uncustomizedViewByBaseViewId });
    				webPartTypeId = b ? XslListWebPartTypeId : ListWebPartTypeId;
    			}
    
    			if (webPartTypeId == ListWebPartTypeId)
    			{
    				//Skipping web part, will not be updated
    				continue;
    			}
    			PropertyInfo propertyInfo = webPart.GetType().GetProperty("NewWebPartTypeId",
    																	  BindingFlags.NonPublic |
    																	  BindingFlags.Instance |
    																	  BindingFlags.SetProperty);
    			propertyInfo.SetValue(webPart, webPartTypeId, null);
    			mgr.SaveChanges(webPart);
    			
    		}
    		if (webPart is XsltListViewWebPart || webPart is ListViewWebPart)
    		{
    			
    			
    			Guid gList = webPart is XsltListViewWebPart
    							 ? ((XsltListViewWebPart)webPart).ListId
    							 : ((ListViewWebPart)webPart).ListId;
    
    			var gView =
    				new Guid(webPart is XsltListViewWebPart
    							 ? ((XsltListViewWebPart)webPart).ViewGuid
    							 : ((ListViewWebPart)webPart).ViewGuid);
    
    			SPList list = web.Lists[gList];
    			SPView view = list.Views[gView];
    
    			view.GetType().InvokeMember("EnsureFullBlownXmlDocument",
    										BindingFlags.NonPublic | BindingFlags.Instance |
    										BindingFlags.InvokeMethod, null, view, null,
    										CultureInfo.CurrentCulture);
    
    
    			
    		}
    	}
    }
    
    

     

    Let me know if you find something better!

     

    BR

    Thursday, January 12, 2012 4:17 PM
  • Hello igby84,

     

    can you explain the "XslListWebPartTypeId" and "ListWebPartTypeId" variable? Where did i get the values for these vars?

     

    Thanks

    Tuesday, January 24, 2012 3:00 PM
  • Hi svenor,
    I don't really remember how I found them, I was almost year ago...

     

    Guid ListWebPartTypeId = new Guid("baf5274e-a800-8dc3-96d0-0003d9405663");
    Guid XslListWebPartTypeId = new Guid("874f5460-71f9-fecc-e894-e7e858d9713e")
    BR
    Tuesday, January 24, 2012 3:27 PM