Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Ashish Patel Jun 14 '11 at 15:23
    
The Delete method is expecting an SPFolder object –  Tim Gabrhel Jun 14 '11 at 15:55
    
I see your problem.. Make sure the Page Layout name is WITHOUT file name extension as documented in SDK. –  Ashish Patel Jun 14 '11 at 16:19
add comment

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
This didn't work. See my Edit 2 –  Tim Gabrhel Jun 14 '11 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 Jun 14 '11 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 Jun 14 '11 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 Jun 14 '11 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 Jun 14 '11 at 16:41
show 9 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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