Monday, December 27, 2010

Populate the web part gallery programatically

Suppose you are deploying everything using solutions. You have lots of webparts. How will you populate the web part gallery using your solution. This will allow the user to just add the web part to the page immediately after deploying wsp.

Step1 - Create a Feature.xml file

Feature Id="D34D00B1-EC5C-4da8-8C50-009CB4B8BB42"
Title="IMCHATFEATURE"
Description="This is the chat Webpart"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="elements.xml"/
ElementFile Location="WPChat.webpart" /
/ElementManifests
/Feature

Step 2: Create an Element File

Elements xmlns="http://schemas.microsoft.com/sharepoint/"
Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE" List="113"
File Url="WPChat.webpart" Type="GhostableInLibrary"
/File
/Module
/Elements

Step 3: Create a .webpart file for your webpart

webParts
webPart xmlns="http://schemas.microsoft.com/WebPart/v3"
metaData
type name="Eversheds.InstantMessaging.WebParts.WPChat, Eversheds.InstantMessaging, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b0c7276684f0a390 "/type
importErrorMessage>Can not import WPChat/importErrorMessage
/metaData
data
properties
property name="Title" type="string"WPChat/property
property name="Description" type="string"A web part which allows the user to chat/property
/properties
/data
/webPart
/webParts


Step 4: Lastly create the .cs file for your web part

namespace Eversheds.InstantMessaging.WebParts
{
public class WPChat : System.Web.UI.WebControls.WebParts.WebPart
{}
}

Place all files in step1,2 and 3 in your feature folder. Deploy using wsp builder. The cs file should be outside the 12 hive folder.

Your web part will be available for the user in the gallery.

No comments:

Post a Comment