I'm trying to add 2 webparts based on the same doclib to my site. Each webpart has it's own view.
The code used to add the webpart is as follows:
ListViewWebPart listviewwebpart = new ListViewWebPart();
listviewwebpart.ListName = doclib.ID.ToString("B").ToUpper();
listviewwebpart.ViewGuid = doclib.Views["Name of view"].ID.ToString("B").ToUpper();
listviewwebpart.Title = 'Unique Title';
listviewwebpart.AllowClose = false;
mgr.AddWebPart(listviewwebpart, "Left", position);
web.AllowUnsafeUpdates = false;
web.Update();
listviewwebpart.Dispose();
mgr.Dispose();
The first time this code runs, it works perfectly.
The second time I get an Error 'The Specified View is invalid'
I've checked the viewcollection of the list. All the views are there. If I do it manually via the UI, there is no problem. And if I switch the order in which the webparts are added, it is always the second one added that throws the error.
This is the stack trace of the error:
Stack trace: at Microsoft.SharePoint.SPViewCollection.get_Item(Guid guid)
at Microsoft.SharePoint.SPList.GetView(Guid viewGuid)
at Microsoft.SharePoint.SPList.GetView(String viewGuid)
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)
Can anyone help me with this?