views:

236

answers:

2

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?

A: 

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!

Lee Richardson
A: 

Lee,

I'm having this exact same problem. Any updates on your findings?

Thanks.

John
Sorry John, the accepted answer is the best I've got. Please edit your answer if you have any updated findings of your own. For instance if setting your view as default does not fix it for you as it did for me, that would be helpful info.
Lee Richardson
Lee I tried many things and then ended up with a requirement / design change. However, I got it to work by calling view.databind(), and then automatically refreshing the screen. I guess you can see why I decided to change the requirements...
John

related questions