How to Automatically Add / Provision a Web Part in a SharePoint 2010 Publishing Site’s Master Page / Page Layout

Written by Denis Stadler on . Posted in Publishing Feature, SharePoint 2010

You might have this requirement in the following scenario: your master page needs to display dynamic information (for example Calendar Web Part, Weather Web Part or Twitter which both can be done with XML Viewer Web Part or dynamic HTML from a SharePoint List displayed with CQWB – Content Query Web Part).

This can be easily solved with SharePoint Designer. Please find bellow the full scenario described:

As a first step you need to create a new place holder within the Site’s Master Page. So, in SharePoint Designer open your Publishing Site Collection and then browse to the current master page.

If you don’t know which master page is your site using, just open the site in Internet Explorer. Browse through Site Settings (click Site Actions Menu -> Site Settings). Then in the Look and Feel section, click Master Page to identify it.

Good. Now let’s go back to SharePoint Designer. Please check out the Master Page and then open it for editing.

The next step is to add a new ASP Content Place Holder. In my example I will add it after the “PlaceHolderMain” Content Place Holder. This place holder will be override in the Page Layout and used to host a new web part zone and the “automatically added” web part which can display my dynamic content:

<asp:ContentPlaceHolder id=”StadlerWebPartArea” runat=”server” />

Now check in the master page, publish it and approve it from the Master Page library.

Go back to SharePoint Designer and in the Page Layouts Library identify the Page Layout(s) which is(are) currently used in you Site Collection. Then check it out for edit. In this example I’m using “Article Left” Page Layout.

Now it is time to override the content place holder created within the master page. Please insert it into the page after the “Register Tag” section.

Then from the Ribbon Menu click Insert and then Web Part Zone Button.

Save the page layout (the quickest way is CTRL+S) and then from the Ribbon Menu click Insert again but this time click on the Web Part Button (I will add a CQWB – content query web part).

Add a Web Part. Save the page, check it in and approve it. Now the work should be done.

To test it go to your publishing site and create a new page using the layout customized earlier. (in my case this is “Article Left”)

After you click Create button you should be redirected to the new page which is in edit mode. In the page you will see your pre-added web part (in my case a CQWP). This web part will be automatically added to all the new pages which will use this layout.

Tags: , , , , ,

Trackback from your site.

Denis Stadler

I'm a technology enthusiast, with more than 10 years of experience in SharePoint and Dynamics CRM projects. To find more details about, please visit the about me page.

Comments (5)

  • Biju

    |

    Hello Denis,

    We have hundreds of published pages. Now we want to add a custom web part to the page layout. I followed the steps you have given. I agree that for new pages created, default web part is available. What about the existing pages. I can’t see the newly added web part in the existing published pages. What should I do to achieve that feature?

    Thanks in advance
    Biju

    Reply

    • Denis Stadler

      |

      There are multiple solutions to achieve this. For example you could write a C# console app that iterates the site pages (in every web, site collection etc.) and adds the web part if the page has the correct page layout. You could use SPLimitedWebPartManager to add the web part.

      Reply

    • AR Pagoti

      |

      You can add the pre-existing webpart to the existing Master Page instead of adding it to the Page Layout and then update the site collections with the Master Page.

      PS: Always work on a copy of the current Master Page.

      Reply

      • Denis Stadler

        |

        It works but I don’t think that it is best practice.

        Reply

Leave a comment

*