Recently some work I was doing required me to include some default web parts in a page layouts.  I understand you can do with a custom site definition, but frankly, I prefer to stay away from those.  So I set about doing it right within my custom layout and here is how I did it.

I started off with Andrew Connell’s steps for doing with SharePoint 2007 (found here  Unfortunately, I made an attempt at this method and wasn’t having much success.  Then I ran across this approach –

While this approach worked for me, I must not that I take issue with just modifying your Page Layout with SharePoint designer.  So, the steps I followed were:

  1. Make a copy of a Page Layout in SharePoint Designer
  2. Using SharePoint Designer, inserted your desired Web Part(s) into any zone on this page layout.
  3. Now, rather than just saving the Page Layout and using this, copy the code inserted into the Page Layout into your Page Layout in your Visual Studio Project (because we all know every customization in SharePoint should be delivered as a .wsp, right?)  Note there are TWO sections of code to copy.
    1. The registration of the web part (at the top of the Page Layout Code)
    2. The Web Pars themselves (pasted into the appropriate Web Part Zone)

Once you have inserted the code you can build and deploy your solution.  Now, every time you create a new page from this page layout, your web parts will be included on the page by default.  One thing I haven’t tried yet is setting any of the web part properties.

If you want to adjust the web part properties within the page layouts (say you want different page layouts with varying values for your web part).  You can simply add the property and value in the web part as show below:

One thing to note is that it is the internal public variable name, not the display name in your web part properties.  Obviously this makes setting properties on custom web parts a little easier than the out of the box web parts where you may have to dig a little bit to figure out what the property variable name actually is.