Wednesday, March 08, 2017

List Views disappear from view selector menu whenever views created programatically

I was developing an automated code which will create views dynamically on SharePoint List. The code was working perfectly for other types of views except for Data Sheet view. The fact that other people are experiencing this makes me suspect of an issue in the API but perhaps there is something we can do to overcome this.

I was surprised when i was adding the view manually through UI was working fine.

This weird issue happens when you are trying to create views programatically. This issue comes in both SSOM or CSOM and it also happens in various types of views, such as Datasheet and Calendar and may be with others. 

The workaround is to trigger an update on the DisableViewSelectorMenu webpart property, which can be achieved by manually editing the view webpart through the user interface, or programatically updating the property and the webpart, as shown below:

From SharePoint UI:



Programtically:













































private static void FixWebPartViewSelector(SPWeb web, SPList list, SPView view) { var pageUrl = web.Url + "/" + view.Url; SPLimitedWebPartManager webpartmanager = web.GetLimitedWebPartManager(pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); XsltListViewWebPart wp = webpartmanager.WebParts[0] as XsltListViewWebPart; wp.DisableViewSelectorMenu = true; webpartmanager.SaveChanges(wp); wp.DisableViewSelectorMenu = false; webpartmanager.SaveChanges(wp); }