How to Add a Webpart to a Site Definition Page

Suppose you’ve created a webpart, which is so cool that you want to have it on every page of your SharePoint site. A nice way to accomplish this is to create your own site definition, which contains your webpart on every page. Does this sound like rocket science? Trust me, it isn’t and I’ll prove it to you. :-) In fact I was a little bit scared of site definitions at first, but once you get used to them they can become very handy! First of all, if you need to learn the basics of creating site definitions, take a look at following article: Creating a Site Definition from an Existing Site Definition.  In this example I’ll modify the site template for a SharePoint Portal site, but you can do the same thing for a Windows SharePoint Services site definition as well. A best-practice is to leave the default site definitions untouched, but on your development box you could choose to alter them anyway to get you started quickly. Just make sure you’ve got a copy, just in case…

 

You can find the Portal site definition in C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\1033\SPS. The 1033 number corresponds with the language of your Portal site definition (1033 is English), SPS corresponds with what kind of page you want to modify (SPS is the Portal stuff, STS is WSS stuff). So let’s open the default.aspx page, but do not open the page by using http://localhost/default.aspx for example, instead open it directly from the SPS folder (otherwise you’d end up with an un-ghosted page). The first thing you need to do is adding the following line to the top of the page:

<%@ Register TagPrefix="WpNs0" Namespace="SuperWebParts" Assembly="SuperWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" %>

Next you need to find the place in the page where you want to insert your webpart. Once you’ve found you’re spot, insert following lines:

<WpNs0:MySuperWebPart runat="server" >

<WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">

  <Title>My Super WebPart</Title>

</WebPart>

</WpNs0:MySuperWebPart>

Notice that the WpNs0 schema reference corresponds with the one you’ve chosen in the Register TagPrefix line at the top of the default.aspx page. Finally save your page and check out the result in a browser.

 

In the <WebPart> node you can specify additional properties for your webpart, for example following lines will display your webpart without title bar.

<WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">

  <Title>My Super WebPart2</Title>

  <FrameType>None</FrameType>

</WebPart>

 

To find out which values you can use inside the <WebPart> element, is to drag-and-drop your webpart on a site the normal way (inside your webrowser); apply all the desired modifications by using the tool pane (click the down arrow and choose “Modify Shared Web Part”). When you’re done click the Apply button so your changes get applied, then click the down arrow in your web part toolbar and choose “Export…”.  This file contains the <WebPart> xml node which you can copy/paste in your default.aspx page. Quite handy isn’t it?

 

One final tip to find the place in the default.aspx page where you want to insert your webpart: open the portal/site page in FrontPage (warning: do not save the page because you’ll end up with an un-ghosted page!!). Then choose the Split view (bottom left) and click the area in which you want to display your webpart. The corresponding lines will be highlighted in the code view! Once again: do not change the page in FrontPage!

If you want to get into site definitions, I highly recommend following articles:

12 Comments

Comments have been disabled for this content.