Tuesday, March 15, 2011

List does not exist error


While adding a new list view web part to a homepage , stuck with the following exception.
List does not exist The page you selected contains a list that does not exist. It may have been deleted by another user.
After some fiddling, found Web.Id for a LVWP is required to resolve this issue. After providing the WebId for the ListViewWebPart, web part adds to the page perfectly.

ListViewWebPart lvp = new ListViewWebPart();
lvp.ListName = Web.Lists["Tasks"].ID.ToString("B").ToUpper()  ;
lvp.ZoneID = "Center";
lvp.Title = "Tasks List";
lvp.WebId = Web.ID;  
SPFile homePage = Web.GetFile("default.aspx");
SPLimitedWebPartManager webmngr = homePage.GetLimitedWebPartManager (PersonalizationScope. Shared);
webmngr.AddWebPart(lvp, "Center", 3);
webmngr.SaveChanges(lvp);

Happy SharePointing :)

No comments: