Monday, August 20, 2007

Defining SharePoint v3 webparts (ASP.NET 2.0 webparts) in the Modules section of ONET.XML

As you probably know there are 2 different base classes that you can use when developing web parts for WSS 3.0 or MOSS 2007. You can either inherit from System.Web.UI.WebControls.WebParts.WebPart which are your ASP.NET 2.0 webparts or you can still use Microsoft.SharePoint.WebPartPages.WebPart (It is considered best practice to build your webparts using the ASP.NET 2.0 way - the other ones are only in there for backwards compatibility). For a thorough discussion about the different ways to build webparts check out Jan's article on MSDN Belux - Web Part Platforms and Development Techniques.


When a site in SharePoint is created it is based on site definition. I'm not going to elaborate about how you can create your custom site definition but I'm going to talk about the Modules section in ONET.XML. In this Modules section, you can specify which files need to be provisioned at site creation - take a look at Using Modules to add files to site definition on MSDN (SPS 2003 documentation - not available for SharePoint 2007 yet) . This article also describes how you use the Modules element to specify which web parts are used on the home page of the default.aspx file in every site.

   <Module Name="Default" Url="" Path="">
      <File Url="default.aspx" NavBarHome="True">
         <View List="104" BaseViewID="0" WebPartZoneID="Left"/>
         <View List="106" BaseViewID="0" WebPartZoneID="Left" WebPartOrder="2"/>
         <AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
            <![CDATA[<WebPart xmlns=""
               <Assembly>Microsoft.SharePoint, Version=, Culture=neutral,
         <View List="103" BaseViewID="0" WebPartZoneID="Right" WebPartOrder="2"/>
         <NavBarPage Name="Home" ID="1002" Position="Start"></NavBarPage>
         <NavBarPage Name="Home" ID="0" Position="Start"></NavBarPage>

Unfortunately, the code listed above only works if you are using V2 webparts - being webparts which inherit from Microsoft.SharePoint.WebPartPages.WebPart. But what if you need to add an ASP.NET 2.0 webpart?  I first thought that I just needed tot change the xmns from to - but that did not work ( ... would probably have been to easy). So what is the correct syntax?

The correct syntax is actually the one which is used for the .webpart files - if these were generated for you (you might have just added a webpart using the approach as described by Mart - Create a SharePoint 2007 webpart step by step ) you can still get at the XML file which you will need to use.

Go to the Web Part Gallery of the site collection which contains your web parts - select Edit document properties for the webpart that you want to include in your site definition. Next click - View XML - this will show you the syntax you will need ...remember that you will need to include the <webParts> ...</webParts> start and end tags. (This is also case sensitive). Beneath is an example of one of the webparts I had created:

    <Module Name="Default" Url="" Path="">
      <File Url="default.aspx" NavBarHome="True">
        <NavBarPage Name="$Resources:core,nav_Home;" ID="1002" Position="Start" />
        <NavBarPage Name="$Resources:core,nav_Home;" ID="0" Position="Start" />
    <AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
    <webPart xmlns="">
    <type name="DolmenWebParts.WebPropViewer, DolmenWebParts, Version=, Culture=neutral, PublicKeyToken=null" />
    <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
        <property name="Title" type="string">WebPropViewer</property>

Happy SharePointing ...


Sheetal Jain said...

This was very helpful. Ran into same problem while creating v3 webparts and surrendered to google to find a quick fix and your blog entry came up

I am still not sure the rationale behind "best practice" to use ASP.NET webparts, which clearly is not implemented keeping SharePoint in mind. For a simple thing like getting class resource path, you need to cast back to SharePoint Web Part Manager.

Thanks again for your contribution to the community

Majid said...

Thanks jopx. great post.

I was wondering if you know how I can set the FrameType to None.
I tried adding it in different places but it didn't work.

jopx said...

Sorry Majid, this is something which I haven't found myself yet...

Anonymous said...

You cannot set the frametype property, it seems it's obsolete, you must set the partchrome and it will work!

Anonymous said...

Your post has saved my life, thank you soooooooooooo much.

Anonymous said...

Brilliant! Thank you. (I can hardly believe that MSDN still have the v2 instructions, even under the WSS 3.0 SDK documentation)

Anonymous said...


Anonymous said...

Just what i needed, thank you very much.

Jithin said...

Thanks a ton.. tat was really helpful.. i'd been wasting my time over this issue.. i shud hv read ur post earlier..