SPList.Views does not retrieve hidden views

Last week I created a list definition with a couple of hidden Views in it, which would only be used in list view webparts.

The Views were defined like this:

 <View BaseViewID="101" Hidden="TRUE" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="FALSE" SetupPath="pagesviewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems_Hidden.aspx">

One of these webparts had to be added programmatically with only the BaseViewID (int) as a reference (101 in the above case).

Now, my normal approach would be to retrieve all views from the list by using SPList.Views and loop trough these views untill I come across the BaseViewID I am looking for.

HOWEVER… When looking in the SPViewCollection retrieved by SPList.Views, the hidden views are nowhere to be found!

Eventually, I found out that we can use the SPList.GetUncustomizedViewByBaseId(int) method, and then get the ViewID out of the PropertiesXml property.

I did it like this:

private Guid GetListViewID(SPList list, string baseViewID)

{

    string properties = list.GetUncustomizedViewByBaseViewId(Convert.ToInt32(baseViewID)).PropertiesXml;

 

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(properties);

 

    Guid listViewId = Guid.Empty;

    string viewUrl = doc.FirstChild.Attributes["Url"] != null ? doc.FirstChild.Attributes["Url"].Value : "";

    if (!string.IsNullOrEmpty(viewUrl))

        listViewId = list.ParentWeb.GetViewFromUrl(string.Format("{0}/{1}", list.RootFolder.Url, viewUrl)).ID;

 

    return listViewId;

}

I don't know if anyone else had encountered this issue, but it seems like something strange to me :)

Maybe I defined my hidden view in an incorrect way, or maybe there is some property or method I overlooked, I don't know.

Anyway, this solution seems to work for me to :) so I hope it helped someone else to ;)

Leave a Reply