Monday, February 28, 2011

Programmatically add web part to SharePoint 2010 web part page

In this article, I will explain how to programmatically add web part to sharepoint web part page.
1. Select SharePoint empty project template then add Module.
2. Right click on Module ,then add ->Existing Item
Browse to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts” select default.aspx page.
3. Update the elements manifest to tell where to find the custom file and where it needs to deploy.
?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">
File Path="Home\default.aspx" Url="default.aspx" IgnoreIfAlreadyExists="TRUE" Type="Ghostable">
AllUsersWebPart WebPartOrder="1"
WebPartZoneID="Left">
![CDATA[
webParts>
webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
metaData>
importErrorMessage>Cannot import this WebPart.
/importErrorMessage>
/metaData>
data>
properties>
property name="HelpUrl" type="string" />
property name="AllowClose" type="bool">True /property>
property name="ExportMode" type="exportmode">All /property>
property name="Hidden" type="bool">False /property>
property name="AllowEdit" type="bool">True /property>
property name="Direction" type="direction">NotSet /property>
property name="TitleIconImageUrl" type="string" />
property name="AllowConnect" type="bool">True /property>
property name="HelpMode" type="helpmode">Modal /property>
property name="CustomProperties" type="string" null="true" />
property name="AllowHide" type="bool">True /property>
property name="Description" type="string">Silverlight webpart /property>
property name="CatalogIconImageUrl" type="string" />
property name="MinRuntimeVersion" type="string" null="true" />
property name="ApplicationXml" type="string" />
property name="AllowMinimize" type="bool">True /property>
property name="AllowZoneChange" type="bool">True /property>
property name="CustomInitParameters" type="string" null="true"/>
property name="Height" type="unit">500px /property>
property name="Width" type="unit">
property name="Title" type="string">Home /property>
property name="TitleBar" type="string">Home /property>
property name="TitleUrl" type="string" >Home /property>
property name="Url" type="string">
/_LAYOUTS/ClientBin/SilverlighApplicationDemo.xap
/property>
property name="WindowlessMode" type="bool">True
/properties>
/data>
/webPart>
/webParts> ]]>
/AllUsersWebPart>
/File>
/Module>
/Elements>

No comments:

Post a Comment