You are here: Home » Adding custom webparts in a Sharepoint Site Definition

Adding custom webparts in a Sharepoint Site Definition

Posted by nsevens
1 Comment »

For many among us, site definitions are a quite unexplored feature of sharepoint.
However, it makes it really easy to add content to custom sites!

Adding a webpart didn't turn out that easy, and especially not when there are custom properties to be set for it …

First of all, let's take a look at how the included Sharepoint webparts are added to a page.

E.g: Titlebar webpart

<AllUsersWebPart WebPartZoneID="TitleBar" WebPartOrder="1">

  <![CDATA[

          <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:twp="http://schemas.microsoft.com/WebPart/v2/TitleBar">

                <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

                <TypeName>Microsoft.SharePoint.WebPartPages.TitleBarWebPart</TypeName>

                <FrameType>None</FrameType>

                <Title>My Procedure</Title>

                <twp:HeaderTitle>Param&#232;tres g&#233;n&#233;raux du dossier en cours ET en fin d'enqu&#234;te au SP</twp:HeaderTitle>

                <twp:HeaderCaption></twp:HeaderCaption>

          </WebPart>

          ]]>

</AllUsersWebPart>

So, this definition defines the following:

  • The webpart zone where the webpart will be placed in will be TitleBar (WebPartZoneID property), and the order will be 1 (WebPartOrder)
  • The webpart XMLNS (XML namespace) and a namespace for the twp: tag.
  • The assembly name of the webpart
  • The classname of the webpart (typename)
  • And then some properties like title, frametype, headertitle, …

Now, however when we create our own webpart in .Net 2.0, we do not need to use the http://schemas.microsoft.com/WebPart/v2 namespace, but the http://schemas.microsoft.com/WebPart/v3 one !

Now, actually the easiest way to find out the correct webpart definition, is to go look for it in Sharepoint itself.

After you installed the webpart in the sharepoint webpart gallery, you can go look for it in the Site Settings.

Site settings –> Webparts (under the Galleries section).

Click the Edit button/image next to the webpart you want to add in the site definition, and then click View XML.

The XML you get, is the definition that has to be included in the CData part of your page!
So, for example you would get:

<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">

  <![CDATA[

    <webParts>

      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

        <metaData>

          <type name="Nick.Sevens.Blog, WebPartExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abc1234567890" />

          <importErrorMessage>Error importing the Web Part.</importErrorMessage>

        </metaData>

        <data>

          <properties>

            <Property Name="Description" Value="Custom webpart added in the site definition." />

            <property name="ChromeType" type="chrometype">TitleOnly</property>

            <property name="TextToDisplay" type="string">Testing the custom property</property>

            <property name="Title" type="string">Custom webpart example</property>

          </properties>

        </data>

      </webPart>

    </webParts>

  ]]>

</AllUsersWebPart>

When implementing this, a custom webpart (Nick.Sevens.Blog.WebPartExample) will be loaded on the screen.

Also, I included a custom property TextToDisplay, which is defined in the Webpart code like this:

[DefaultValue("")]

[Personalizable(PersonalizationScope.Shared), WebBrowsable, WebDisplayName("Text to display"),

WebDescription("The text to display in the custom webpart.")]

public string TextToDisplay

{

    get { return _textToDisplay; }

    set { _textToDisplay = value; }

}

This way, the property TextToDisplay of the Custom Webpart is accessible through the site definition!

 PS: Do note, that when updating the site definition, the already created pages won't be automatically updated with the changes! This is due to the fact that all webpart information, even if created through a site definition, is stored in the database (for personalization reasons).

1 Comment

Your email is never shared.
Required fields are marked *




Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>