In a publishing site, I want to delete a specific page layout. This code snippet is within a FeatureDeactivating event. layout.Folder is returning null, but I need the folder to delete the PageLayout.

SPSite site = (SPSite)properties.Feature.Parent;
if (PublishingSite.IsPublishingSite(site))
   PublishingSite pubSite = new PublishingSite(site);

   SPContentType ct = site.RootWeb.ContentTypes["Custom Page"];

   string layoutURL = site.ServerRelativeUrl + "/_catalogs/masterpage/" + pageLayoutName;
   PageLayout layout = pubSite.PageLayouts[layoutURL];
   SPFolder folder = web.GetFolder("_catalogs/masterpage");
   pubSite.PageLayouts.Delete(pageLayoutName, folder);

When I try to run the code, I get an ArgumentOutOfRangeException:fileUrl

Edit 1

I missed 2 lines of code. I'm tried to pass in a folder object (because layout.Folder is null). The passed in folder object returns the ArgumentOutOfRangeException.

Edit 2

Following James' suggestion, I modified the code as the following:

string layoutURL = site.ServerRelativeUrl + "/_catalogs/masterpage/" + pageLayoutName;
PageLayout layout = pubSite.PageLayouts[layoutURL];
SPList masterpageGallery = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
pubSite.PageLayouts.Delete(pageLayoutName, masterpageGallery.RootFolder);

But I'm still getting the same error

  • It appears to be a URL format problem. While debugging did you check format of folder.Files[0].Url and format of layoutURL? the Delete method expects a site-relative URL. Commented Jun 14, 2011 at 15:23
  • The Delete method is expecting an SPFolder object Commented Jun 14, 2011 at 15:55
  • I see your problem.. Make sure the Page Layout name is WITHOUT file name extension as documented in SDK. Commented Jun 14, 2011 at 16:19

2 Answers 2


The MasterPage Gallery is actually a document library, you want to remove the page layout from the RootFolder of that library (which you can get via List.RootFolder).

Use GetCatalog to get the List, then get the folder via RootFolder.

  • This didn't work. See my Edit 2 Commented Jun 14, 2011 at 15:50
  • When you use my suggestion, where is the error thrown? Looking more closely, I'm wondering how you get the pageLayoutName variable, too. Make sure it ends in aspx.
    – James Love
    Commented Jun 14, 2011 at 15:55
  • 1
    You can try, but I have a feeling you'll need to delete it from the PageLayouts collection, otherwise when you go to "Create a new Publishing Page", you'll get an ASP.NET error saying sharepoint can't find the list item associated with a page layout.
    – James Love
    Commented Jun 14, 2011 at 16:20
  • 2
    I just had a look in Reflector - seem the PageLayoutCollection.Delete method just deletes the SPFile anyway! Doesn't do any checking on usage of the page layout at all! I'm not sure if a Page whose layout is missing is automatically detatched if you try to open it, or throws an error. The Publishing Infrastructure is so flimsy imo.
    – James Love
    Commented Jun 14, 2011 at 16:31
  • 1
    That shouldn't be a problem, you just need to a SiteDataQuery where the content type starts with the associated content type of the page layout. I just got that same error you got when trying to delete the page layout, looks like it might be below the Managed API where it checks its usage (which, to be fair, would be a 2-line SQL query compared to the SPSiteDataQuery stuff you're gonna have to do to get the same results).
    – James Love
    Commented Jun 14, 2011 at 16:41

I had the same problem as you have but I got the solution by below code.

            SPWeb web = properties.Feature.Parent as SPWeb;
            SPList list = web.GetCatalog(SPListTemplateType.MasterPageCatalog);
            PublishingSite pubSite = new PublishingSite(web.Site);
            pubSite.PageLayouts.Delete("ArchivePagePL", list.RootFolder);  

This worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.