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

Posted by Lee Richardson on Stack Overflow See other posts from Stack Overflow or by Lee Richardson
Published on 2010-05-04T19:34:36Z Indexed on 2010/05/04 19:38 UTC
Read the original article Hit count: 906

Filed under:

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. I have no idea what LightweightViews is supposed to mean and I'm running out of ideas. Anyone else got any?

© Stack Overflow or respective owner

Related posts about sharepoint2010