The .webpart file

Background

The .webpart file is an XML file containing metadata about the Web Part. In previous versions of SharePoint this file had an extension of .dwp. Although these files may look similar it’s a crucial difference between those files. The .webpart file uses the http://schemas.microsoft.com/WebPart/v3 schema and the .dws file useshttp://schemas.microsoft.com/WebPart/v2. The v2 schema is intended for Web Parts derived from theMicrosoft.SharePoint.WebPartPages.WebPart class, which is the base class used for developing Web Parts for previous SharePoint versions. When developing Web Parts using the .NET 2.0 WebPart class (System.Web.UI.WebControls.WebParts.WebPart) the v3 schema must be used.

Structure

The .webpart file uses the following structure, with some elements left out for later posts.

  • webParts
    • webPart
      • metaData
        • type
        • importErrorMessage
      • data
        • properties
          • property

The metadata element contains information about the Web Part; the type element must contain a name attribute which must be the full name of the Web Part class, and the importErrorMessage element contains an error message that is displayed if the import of the Web Part fails.

The data section contains information about the default values of the Web Part properties, each default value is set using the property element. Each property element has a name attributetype attribute and a value; the name property must match an exposed Web Part property and the type must be the Type of the property. The type can be expressed as a fully qualified type name or one of the built-in shortcuts (string, int, bool, double, single etc). The value of the type property is the default value.

Sample

Here is a sample of a .webpart file, taken from the TweetPart in the previous Web Part Properties posts.

  1: <?xml version="1.0" encoding="utf-8"?>
  2: <webParts>
  3:   <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  4:     <metaData>
  5:       <type name="TweetPart.TwitterWebPart, TweetPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cfa2cd081d8900d7" />
  6:       <importErrorMessage>Error importing Web Part&lt/importErrorMessage>
  7:     &lt/metaData>
  8:     <data>
  9:       <properties>
 10:         <property name="Title" type="string">TweetPart&lt/property>
 11:         <property name="Description" type="string">Sample Web Part by Wictor Wilén&lt/property>
 12:         <property name="ChromeState" type="chromestate">Normal&lt/property>
 13:         <property name="AllowZoneChange" type="bool">True&lt/property>
 14:         <property name="AllowHide" type="bool">True&lt/property>
 15:         <property name="ExportMode" type="exportmode">NonSensitiveData&lt/property>
 16:         <property name="TitleIconImageUrl" type="string">/_layouts/images/TweetPart/TweetPart_16.png&lt/property>
 17:         <property name="CatalogIconImageUrl" type="string">/_layouts/images/TweetPart/TweetPart_16.png&lt/property>
 18:         <property name="Username" type="string">wictor&lt/property>
 19:         <property name="Mode" type="TweetPart.TwitterTimeLineMode">User&lt/property>
 20:         <property name="TweetCount" type="int">10&lt/property>
 21:       &lt/properties>
 22:     &lt/data>
 23:   &lt/webPart>
 24: &lt/webParts>

Note: it is not necessary to add all of your Web Part properties to the .webpart file, unless a default value is needed.

Web Parts created using the SharePoint WebPart class can also define default values using the DefaultValue attribute, like this:

  1: [...]
  2: [DefaultValue("default value")]
  3: public string Text {
  4:     get;
  5:     set;
  6: }

Another Sample:

Install and customize

Every Web Part needs one of these .webpart files to work. To get your Web Part to work you have to register the class in the correct way and the add the .webpart file to the Web Part Gallery. This is normally done when you deploy your Web Part feature.

If the Web Part has a lot of custom properties you can help your users by editing the .webpart file and customize the default values. Just go to the Web Part Gallery and choose to Export the Web Part. Then you just edit the file with your favorite XML editor and upload it once again to the Web Part Gallery.

Export a Web Part

Tip: If you don’t want to manually edit the file you can always add the Web Part to a page and then edit the properties and then use the Export menu option on the Web Part.

Cited : http://www.wictorwilen.se/Post/Web-Part-Properties-part-3-the-webpart-file.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s