I've This Code to Add Page from Page Layout

 using (SPSite site = new SPSite(w.Url))
            using (SPWeb web = site.OpenWeb())
                PublishingSite pSite = new PublishingSite(site);
                SPContentType ctype = pSite.ContentTypes["Page"];
                PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
                PageLayout pageLayout = pageLayouts["/_catalogs/masterpage/gallery.aspx"];
                PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
                PublishingPageCollection pPages = pWeb.GetPublishingPages();

                    PublishingPage pPage = pPages.Add("photogallery.aspx", pageLayout);

                    SPListItem newpage = pPage.ListItem;
                    newpage["Title"] = "Gallery" + " | " + web.Name;
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Gallery Page Successfuly created for " + web.Name);

                    newpage.File.CheckIn("all looks good");
                    newpage.File.Publish("all looks good");
                catch (Exception e)
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The Gallery Page is Already Created for this site! You can Recreate It");  


How can i apply Layout to existing Default homepage Site/subsite/default.aspx ?

pWeb.DefaultPage //returns the Default.aspx page

but i dont know how to change its layout


The default.aspx page is a web part page. You can change the layout of a layout page and not the webpart page. If you requirement is to set a layout page as the home page of site, then you can use following code sample:

using (SPSite site = new SPSite("http://YourSite"))
    using(SPWeb web = web = site.OpenWeb("SubWeb"))
        SPFolder rootfolder = web.RootFolder;
        rootfolder.WelcomePage = "Pages/Home.aspx";


To update the "SharePoint Server Publishing" Feature in all subsites of a site collection, use follwoing PS code:

$site = Get-SPSite "http://aissp2013/sites/team"
foreach ($web in $site.AllWebs)
    $FeatureID = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" 
    Write-Host "Enabling feature in $web"
    Enable-SPFeature -Identity $FeatureID -Confirm:$false -Url $web.url -ErrorAction SilentlyContinue
Write-Host "Finished!"
  • The 'Pages' Document Libarary is missing :( – Ahmed Aborady Feb 18 '15 at 14:36
  • it thew that exception – Ahmed Aborady Feb 18 '15 at 14:36
  • I found that server publishing feature is disabled on all sub sites how can i enable them on all sub sites ? – Ahmed Aborady Feb 18 '15 at 14:43
  • Use a PowerShell code to loop through all sub sites and update the feature. – Nadeem Yousuf-AIS Feb 18 '15 at 14:56
  • i used This script can u take a look ? its loading for very long time no sigle writeLine in powershell sharepointfix.com/2011/01/… – Ahmed Aborady Feb 18 '15 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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