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?
To make it work, just do the following:
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.
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:
Best I can do, sorry. If you're having this problem, good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With