Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The specified view is invalid" in call to LimitedWebPartManager.AddWebPart in SharePoint 2010

This code used to work in WSS 3.0 / MOSS 2007 in FeatureReceiver.FeatureActivated:

using (SPLimitedWebPartManager limitedWebPartManager = Site.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared)) {
    ListViewWebPart listViewWebPart = new ListViewWebPart {
        Title = title,
        ListName = list.ID.ToString("B").ToUpper(),
        ViewGuid = view.ID.ToString("B").ToUpper()
    };
    limitedWebPartManager.AddWebPart(listViewWebPart, zone, position);
}

I'm trying to convert to SharePoint 2010 and it now fails with:

System.ArgumentException: The specified view is invalid.
at Microsoft.SharePoint.SPViewCollection.get_Item(Guid guid)
at Microsoft.SharePoint.WebPartPages.ListViewWebPart.EnsureListAndView(Boolean requireFullBlownViewSchema)
at Microsoft.SharePoint.WebPartPages.ListViewWebPart.get_AppropriateBaseViewId()
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)

Interestingly enough when I run it from a unit test it works, it only fails in FeatureActivated. When I debug with Reflector it is failing on this line:

this.view = this.list.LightweightViews[new Guid(this.ViewGuid)];

list.LightweightViews only returns one view, the default view, even though list.Views returns all of them. When running from a unit test LightweightViews returns all of my views. I have no idea what LightweightViews is supposed to mean and I'm running out of ideas. Anyone else got any?

like image 488
Lee Richardson Avatar asked May 04 '10 19:05

Lee Richardson


2 Answers

To make it work, just do the following:

  • Do not set the viewguid property of the listviewwebpart object (leave it blank)
  • call the AddWebpart method

It will generate a new viewguid associated to a new hidden view. Then if you want to customize this view, retrieve it from the list and customize it.

like image 102
Tonio757 Avatar answered Oct 19 '22 17:10

Tonio757


Hopefully no one ever has this problem or even sees this question. In the unfortunate event you get the same problem I have no specific solution. It eventually just started to work for me (8 hour later). I can tell you what I did right before it started working and hopefully it will help:

I went in through the UI and set the view that I was trying to set the list view web part to as the default view. I believe that's what fixed it and I have no idea why.

Some other notes on the problem:

  • I create all my lists and views through code
  • RunWithElevatedPrivileges did not help
  • Instantiating a new SPWeb in feature activated did not help
  • Setting ListViewXml = view.HtmlSchemaXml instead of setting ViewGuid made it not crash but the view was wrong when this code executed in FeatureActivated but correct when executed in a unit test.

Best I can do, sorry. If you're having this problem, good luck!

like image 3
Lee Richardson Avatar answered Oct 19 '22 19:10

Lee Richardson