views:

275

answers:

2

Hi,

How can I determine programmatically what master pages (custom and OOTB) that are available for to use for a web site in SharePoint?

Thanks, MagicAndi

A: 
Wim Hollebrandse
Hi Wim, could you please add some more detail? What object are you checking the base type of? Thanks.
MagicAndi
Wim, Thanks for elaborating. This solution might be applicable for a ASP.NET website, but wouldn't help in terms of developing against SharePoint. The Master pages I am referring to are stored within a SharePoint list.
MagicAndi
+5  A: 

I came up with this solution, making use of a SPQuery object to query the team site collection's Master Page Gallery list:

try
{
    using (SPSite site = new SPSite(this.ParentSiteUrl))
    {
            using (SPWeb web = site.OpenWeb())
            {
                SPList myList = web.Lists["Master Page Gallery"];
                SPQuery oQuery = new SPQuery();
                oQuery.Query = string.Format("<Where><Contains><FieldRef Name=\"FileLeafRef\" /><Value Type=\"File\">.master</Value></Contains></Where><OrderBy><FieldRef Name=\"FileLeafRef\" /></OrderBy>");
                SPListItemCollection colListItems = myList.GetItems(oQuery);

                foreach (SPListItem currentItem in colListItems)
                {
                   // Process master pages
                }
        }
    }
}
catch (Exception ex)
{
}
MagicAndi