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);
folder.Files[layoutURL].Delete();
}
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