开发者

Retrieve all Master Pages for a SharePoint Site?

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


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)
{
}


Use reflection and check whether the type's base type equals

System.Web.UI.MasterPage
.

So something along the lines of:

foreach(Type t in Assembly.GetExecutingAssembly().GetTypes())
{
  if (t.BaseType==typeof(MasterPage))
  {
      // do something, add to collection - whatever
  }
}

But, depending on in what assembly your MasterPages are defined, and the fact it iterates over all the types in a specific assembly, it may definitely not be the best solution.

I am blissfully ignorant about SharePoint, but this solution is somewhat more generic I guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜