If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Wednesday, October 26, 2011

SharePoint Check "page layout"

In code, I had a reference to an SPFile object and I wanted to check if the SPFile was a page layout file.

I used code (similar to the sample below) to do this. It checks to see if the file's parent list is the master page / page layout library.

Maybe you know a better way?


string url = " -- ABSOLUTE PATH TO YOUR FILE -- ";

using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPList masterPagelist = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
SPFile file = web.GetFile(url);

if (file.Item.ParentList.ID == masterPagelist.ID)
{
// The file is in the master page library.
}
}
}

No comments:

Post a Comment