MOSS Site Navigation – Obtain Page layouts programmatically

Page copy protected against web site content infringement by Copyscape

MOSS Site Navigation – Obtain Page layouts programmatically 

Here is the code snippet that will help you obtain the site navigation structure of an existing site (this will be useful when you are trying to automate deployment of site structure to the target server)

Assuming web is of type SPWeb, SW is StreamWriter that writes to a xml file

 PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
            
   try
      {
          if (PublishingPage.IsPublishingPage(pubWeb.DefaultPage.Item))
          {
               SW.WriteLine("<page site=\"");
              \\ this will give you the site page name
              SW.Write(web.ServerRelativeUrl.Remove(0, 1));
              SW.Write("\" pagelayout=\"");
              \\ this will give you the Page layout name
              SW.Write(PublishingPage.GetPublishingPage(pubWeb.DefaultPage.Item).Layout.Name);
              \\ this will give you the Page title
              SW.Write("\" title=\"");
              SW.Write(pubWeb.DefaultPage.Title); 
              SW.WriteLine("\" />");

  
           }
      } 

I have left out the exception handling part and other detailed code. This is just to give you an idea about the methods available with the PublishingWeb object that will fetch you the site names/page layouts/title details.

Remember, this code snippet has to be called recursively for each SPWeb available under SPWeb.Webs