Wednesday, February 4, 2015

SharePoint 2013 - Adding a ListViewWebPart throws error "The specified view is invalid"

Today I encounted an issue where I was programatically adding a ListViewWebPart to a page and getting an exception "The specified view is invalid".

The code throwing the exception was

            var listViewWebPart = new ListViewWebPart 
            {
                ListId = list.ID, 
                ChromeType = PartChromeType.None,
                ViewGuid = list.Views[Constants.ViewNames.DocumentLibraryAllDocumentSets].ID.ToString("B").ToUpper()
            };
            webPartManager.AddWebPart(listViewWebPart, "Top", 1);

The solution was to change ListViewWebPart to XsltListViewWebPart. Once I did that the problem went away.