Tuesday, April 15, 2008

Creating WebPart instances in Custom Site Definitions

Cool Stuff! I have just found or I should say "I just came to know" that we can create a webpart instance on the default page of a site right in the Site Definition. Consider a scenario where you are having a Custom Site Definition and you wish to put some standard webparts for all the users when a site is created using this Site Definition->Site Template. You can do this in two ways one is the through the object model using SPLimitedWebPartManager class and another is via CAML in site definition. I will explain via CAML way in this post:

If you have seen my previous post on Site Def architecture, we have got Modules node:

<Modules> <Module Name="Default" Url="" >
<File Url="default.aspx" NavBarHome="True" Type="Ghostable"> </File>
</Module>
</Modules>

Now under File node we have <AllUsersWebPart> node available. This is the key to add any webpart instances. Below is the example:

<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="0">
<![CDATA[
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"
xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image%22&gt; <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly> <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName> <FrameType>None</FrameType>
<Title>Home Page</Title> <iwp:ImageLink>/_layouts/images/CONTACT.PNG</iwp:ImageLink> </WebPart> ]]>
</AllUsersWebPart>

No comments: