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.

<Modules>
   <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="
http://schemas.microsoft.com/WebPart/v2"
               xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image">
               <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral,
               PublicKeyToken=71e9bce111e9429c</Assembly>
               <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName>
               <FrameType>None</FrameType>
               <iwp:ImageLink>_layouts/images/homepage.gif</iwp:ImageLink>
               </WebPart>]]>
         </AllUsersWebPart>
         <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>
       </File>
    </Module>
</Modules>

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 http://schemas.microsoft.com/WebPart/v2 to http://schemas.microsoft.com/WebPart/v3 - 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:

  <Modules>
    <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">
    <![CDATA[
    <webParts>
    <webPart xmlns="
http://schemas.microsoft.com/WebPart/v3">
    <metaData>
    <type name="DolmenWebParts.WebPropViewer, DolmenWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
        <properties>
        <property name="Title" type="string">WebPropViewer</property>
        </properties>
    </data>
    </webPart>
    </webParts>
      ]]>
    </AllUsersWebPart>     
    </File>     
   </Module>
 </Modules>

Happy SharePointing ...

9 comments:

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...

Enrique Blanco 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...

Thanks

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..