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>
How to uninstall an assembly from GAC (Global assembly Cache)
In this article, I will explain different ways to uninstall an assembly from GAC
1. Using the Windows interface
a. Navigate to the GAC, which is located at %System Drive%\Windows\Assembly.
b. Right-click assembly file that is included in your application, click Uninstall, and then click yes to confirm.
2. Using the command line
a. Click Start, then point to Command Prompt, and run it as administrator.
b. At the command prompt, Navigate to following path:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
c.Enter following command gacutil /u "fully qualified assembly name".
In this command, fully qualified assembly name is the name of the assembly to uninstall from the GAC.
For e.g.
gacutil /u "FirstApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef4b5fc568888"
Here FirstApplication is assembly name