Set Available PageLayouts for your Site

Below is a code snippet for setting available page layouts for your site:

 

List<String> strPageLayoutsToPersist =

        new List<string> { "Home Page", 
                           "General Detail Page", 
                           "Email Me Page", 
                           "Category Page" 
                         };
using (SPWeb spWeb = spSite.RootWeb)
{
        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(spWeb);
        
        // Get avaialble page layouts for our content types
        PageLayout[] myPageLayouts = pubWeb.GetAvailablePageLayouts().Cast<PageLayout>()
            .Where(p => strPageLayoutsToPersist.Contains(p.AssociatedContentType.Name))
            .ToArray();
 
        // Set the available page layouts to our content type
        pubWeb.SetAvailablePageLayouts(myPageLayouts, false);
 
        // Update the web
        pubWeb.Update();
}

 

This will hide every page layouts except those you specify in the strPageLayoutsToPersist

Using query parameters in SharePoint page layouts

I had a strange case today where SharePoint was throwing this error

No item exists at http://mytestsite?id=4.  It may have been deleted or renamed by another user.

I use pagination in one of my page layouts and I got the above error when I select a page in the preview display mode when I check in draft. There was nothing wrong in the code that I could change as things seem to work fine if I publish the page.

Googling, I found this thread at the MSDN forums

Never put the same name to your parameters what sharepoint is using as internal or display name.
Give your own name then you never get this problem.

like:- Instead of  http://server/Pages/Publish.aspx?publishFileName=folderName/subfolderName/filename&ID=12

use this  http://server/Pages/Publish.aspx?publishFileName=folderName/subfolderName/filename&PublishItemID=12

Yep, things worked fine for me after changing my query parameter 'id' to something else, like 'pageid'

http://mytestsite?id=4 to http://mytestsite?pageid=4

Its very bad that SharePoint internally uses the query parameter "id" for its internal use. I am sure many developers would choose 'id' as the query parameter for anything they start with. I mean, something like "spid" or "sharepointId" would have been fine, right?

 P.S - http://mytestsite is some randomly chosen name


Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.