sharepoint 2007 – 2010 web part development
Write Custom WebParts for SharePoint 2007
The popularity of SharePoint Portal Server 2003 led Microsoft to tightly integrate the next SharePoint version, Microsoft Office SharePoint Server (MOSS) 2007, with its ASP.NET 2.0 WebPart framework. This tight integration enables the ASP.NET and SharePoint developer to do a number of things that previously weren’t possible. For instance, you can plug in your own Membership provider and implement custom authentication. MOSS 2007 also resolves one of SharePoint Portal Server 2003′s main problems: It relies on an ISAPI filter to route its requests. Microsoft replaced the ISAPI filter with ASP.NET’s HttpHandlers and HttpModules in the new version.
A Tale of Two Base Classes
- A SharePoint Web page is a combination of an ASPX file and a precompiled DLL. If you have customized the page (“unghosted” it), the changes are stored in a database. So, the final ASPX is a combination of the uncustomized ASPX on the file system and the customization changes in the database. You usually find the precompiled class in the Microsoft.SharePoint.ApplicationPages namespace. Various SharePoint pages contain a complicated inheritance structure, but eventually they all end up inheriting from System.Web.UI.Page, which is part of the ASP.NET 2.0 framework. So, in short, all SharePoint ASPX pages are really ASP.NET 2.0 WebPages.
- SharePoint comes with a number of Web controls—not WebParts, just Web controls, that also inherit from System.Web.UI.Control.
- The SharePoint WebPartManager is a sealed class called SPWebPartManager. It inherits from System.Web.UI.WebControls.WebParts.WebPartManager, which is the ASP.NET 2.0 WebPartManager.
- A SharePoint WebPartZone is a public sealed class called Microsoft.SharePoint.WebPartPages.WebPartZone, which inherits from System.Web.UI.WebControls.WebParts.WebPartZone.
- A SharePoint WebPart inherits from an abstract base class called Microsoft.SharePoint.WebPartPages.WebPart, which inherits from System.Web.UI.WebParts.WebPart.
Developing a Web Part for Moss 2007
We will be learning how to develop a Web Part that loads in a SharePoint site. To begin creating a web part:
- VS.NET 2005 installed.
- “Web Part Project Library” installed on your system.
- Start VS.NET 2005 and create a new project.
- Select Project Type as Visual C#–>SharePoint.
- Visual Studio Installed templates as Web Part.
- Change Name to Web Part Basics.
- Change Location to e:\Webpartbasics (or appropriate drive letter).
- Change Solution name to FileUploadWebPart.
The Render Method
The Web Part base class overrides the Render method of System.Web.UI.Control because the Web Part infrastructure needs to control the rendering of the Web Part contents. For this reason custom Web Parts must override the Render method of the Web part base class.
/*Creation Log**********************************************************<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />
* Author Company : Amar Infotech // www.amarinfotech.com
* Creation Date : 15-05-2009
* FileName : Webpartbasics.cs
* Class : Webpartbasics
* Description : Tracking the web part life cycle.
A Web Part is an add-on ASP.NET technology to Windows SharePoint Services
***********************************************************************/
using System;
using System.Web.UI;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Webpartbasics
{
[Guid("c07e5146-b025-4f9b-b0a4-a0fc5cb2cfd6")]
public class Webpartbasics : System.Web.UI.WebControls.WebParts.WebPart
{
//varible declaration
private string _strResult;
private TextBox _tbxText;
private Button _btnShow;
private Label _lblErrMsg;
/// <summary>
/// 1 st EVENT IN WEB PART MANAGER
/// During the initialization, configuration values that were marked as webbrowsable
/// and set through the webpart task pane are loaded to the webpart
/// </summary>
/// <param></param>
protected override void OnInit(EventArgs e)
{
try
{
this._strResult += “onInit Method <br>”;
base.OnInit(e);
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// 2 nd EVENT IN WEB PART MANAGER
///
/// Viewstate is a property inherited from System.Web.UI.Control .
/// The viewstate is filled from the state information that was previously serialized
/// would like to persist your own data within webpart
/// </summary>
/// <param></param>
protected override void LoadViewState(object savedState)
{
try
{
_strResult += “LoadViewState<br>”;
object[] viewstate = null;
if (savedState != null)
{
viewstate = (object[])savedState;
base.LoadViewState(viewstate[0]);
_strResult += (string)viewstate[1] + “<br>”;
}
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
} /// <summary>
/// 3 rd EVENT IN WEB PART MANAGER
/// All of the constituent controls are created and added to the controls collection
/// </summary>
protected override void CreateChildControls()
{
try
{
_strResult += “CreateChildControls<br>”; //creating error label controls
this._lblErrMsg = new Label();
this._lblErrMsg.ID = “lblErrMsg”;
this._lblErrMsg.Text = string.Empty;
this.Controls.Add(_lblErrMsg); //creating text controls
this._tbxText = new TextBox();
this._tbxText.ID = “tbxText”;
this._tbxText.Text = string.Empty;
this.Controls.Add(_tbxText); //creating button controls
this._btnShow = new Button();
this._btnShow.ID = “btnShow”;
this._btnShow.Text = “Check Text Value”;
this._btnShow.Click += new EventHandler(_btnShow_Click);
this.Controls.Add(_btnShow);
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// butoon click event
/// </summary>
/// <param></param>
/// <param></param>
private void _btnShow_Click(object sender, EventArgs e)
{
try
{
this._strResult += “button click event has fired<br>”;
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// 4 th EVENT IN WEB PART MANAGER
/// Perform actions common to all requests, such as setting up a database query
/// At this point, server controls in the tree are created and initialized, the state is
restored,
/// and form controls reflect client-side data.
/// </summary>
/// <param></param>
protected override void OnLoad(EventArgs e)
{
try
{
this._strResult += “Onload<br>”;
base.OnLoad(e);
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// 5 th EVENT IN WEB PART MANAGER
/// Perform any updates before the output is rendered.
/// Any changes made to the state of the control in the pre-render phase can be
saved
/// while changes made in the rendering phase are lost.
/// </summary>
/// <param></param>
protected override void OnPreRender(EventArgs e)
{
try
{
this._strResult += “OnPreRender<br>”;
base.OnPreRender(e);
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// 6 th EVENT IN WEB PART MANAGER
/// store cutom data within a web part’s viewstate
/// The ViewState property of a control is automatically persisted to a string
object after this stage.
/// This string object is sent to the client and back as a hidden variable.
/// For improving efficiency, a control can override
/// the SaveViewState method to modify the ViewState property.
/// once viewstate is saved,the control webpart can be removed from the memory
of the server.
/// webpart receives notification that they are about removed from memory
through dispose event
/// </summary>
/// <returns></returns>
protected override object SaveViewState()
{
this._strResult += “SaveViewState<br>”;
object[] viewstate = new object[2]; viewstate[0] = base.SaveViewState();
viewstate[1] = “MyTestData”;
return viewstate;
} /// <summary>
/// 7 th EVENT IN WEB PART MANAGER
/// Generate output to be rendered to the client.
/// you can create user interface of your webpart using html table.
/// You can apply your css classes here itself
/// </summary>
/// <param></param>
public override void RenderControl(HtmlTextWriter writer)
{
try
{
//this method ensure all created child controls
EnsureChildControls();
this._strResult += “RenderControl<br>”;
//table start
writer.Write(“<table id=’tblTest’align=’center’ cellpadding=’0′ cellspacing=’0′
border=’1′ width=’100%’>”);
writer.Write(“<tr>”);
writer.Write(“<td>”);
writer.Write(_strResult);
writer.Write(“</td>”);
writer.Write(“</tr>”); //row 2
writer.Write(“<tr>”);
writer.Write(“<td>”);
this._tbxText.RenderControl(writer);
writer.Write(“</td>”);
writer.Write(“</tr>”); //row 3
writer.Write(“<tr>”);
writer.Write(“<td>”);
this._btnShow.RenderControl(writer);
writer.Write(“</td>”);
writer.Write(“</tr>”); //row 4
writer.Write(“<tr>”);
writer.Write(“<td>”);
this._lblErrMsg.RenderControl(writer);
writer.Write(“</td>”);
writer.Write(“</tr>”);
writer.Write(“</table>”);
//table end
}
catch (Exception ex)
{
this._lblErrMsg.Text = ex.Message.ToString();
}
}
/// <summary>
/// 8 th EVENT IN WEB PART MANAGER
///
/// Perform any final cleanup before the control is torn down.
/// References to expensive resources such as database connections must be
/// released in this phase.
/// </summary>
public override void Dispose()
{
base.Dispose();
}
/// <summary>
/// 9 th EVENT IN WEB PART MANAGER
///
/// Perform any final cleanup before the control is torn down.
/// Control authors generally perform cleanup in Dispose and do not handle this
event
/// The webpart removed from memory of the server
/// Generally webpart developer do not need access to this event because all
/// the clean up should have been accomplish in dispose event
/// </summary>
/// <param></param>
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
}
}
}
Deploy File Upload Web Part in the SharePoint Server
- Right click solution file then select properties.
- Select Debug –> Start browser with URL selects your respective SharePoint Site.
Select Signing Option
- Ensure the strong name key and sign the assembly check box.
- Right Click properties and select Deploy.

- The Web Part is automatically deployed to the respective site.
- Deploy will take care of .Stop IIS, buliding solution, restarting solution, creating GAC, restarting IIS like that.
- Now you should have this web part in your SharePoint page.

The final web part is added to the site. All events displayed in web part have order.
Categories
- ..Online Songs / Videos (40)
- ..Gujarati Garba (26)
- A R Rahman (1)
- Akbar & Birbal (2)
- Bal Mandir (1)
- Ganesh-aartis (1)
- Gujarati Dayro (1)
- Gujarati Jokes (1)
- Shiv Shankar (1)
- Shree Krishna (3)
- shreenathji's (2)
- Titodo Raas (1)
- Andhra Pradesh (11)
- STD & Zip Codes (11)
- Commonwealth Games 2010 (14)
- Venue (8)
- Developer (195)
- .NET (5)
- Android Developer (2)
- DNN (7)
- Custom Module (3)
- Skins (2)
- Templates (2)
- Tutorial (2)
- Domain Registration (1)
- Expedia Affiliate (3)
- Facebook (1)
- Hire Developer (45)
- infopath web parts (14)
- Joomla Developer (8)
- Linux Hosting Gujarat (1)
- Magento Developer (59)
- Microsoft .Net (20)
- MOSS 2007 – 2010 (21)
- MS SQL2008 (2)
- php (8)
- sharepoint 2007 (20)
- SharePoint 2010 (6)
- TYPO3 (9)
- Web Designing (29)
- Web Part Development (1)
- Window Hosting Gujarat (1)
- Wordpress (7)
- X-cart (1)
- Yii Framework (21)
- Education and training (1)
- Festival (53)
- ..Mahashivratri (4)
- Diwali (4)
- Ganesh Chaturthi (8)
- Independence Day (1)
- jagannath rath yatra (8)
- Jalaram Mandir (5)
- Janmashtami (6)
- JYOTIRLINGA (8)
- Lili Parikrama (2)
- Raksha Bandhan (1)
- Republic Day (3)
- Shamlaji Melo (3)
- Fun & Good Stories (13)
- Gujarat (125)
- Ahmedabad (7)
- Amreli (3)
- Communities (2)
- Fruits (2)
- Mango (2)
- Gujarati Month (4)
- Handicraft (5)
- Junagadh (5)
- Sasan Gir (2)
- khodal-dham (20)
- Mehsana (2)
- Politics (14)
- BJP (10)
- List of C.M. (1)
- list of MLA (1)
- Members of Parliament (3)
- Rajkot (11)
- River (1)
- STD & Zip Codes (19)
- Surat (6)
- GUJARAT BEACHES (5)
- Ahmedpur Mandvi (1)
- Beyt Dwarka Beach (1)
- Kutch Mandvi Beach (2)
- Nagoa Beach, Diu (1)
- Gujarati Businessmen (1)
- Jamsetji Tata (1)
- Hindi Movies (2)
- India (7)
- Satyamev Jayate (2)
- IPL 2011 (16)
- IQ test (2)
- News (71)
- Abdul Kalam's Letter (1)
- Ahmedabad (1)
- Ayodhya verdict (2)
- Bom Sabado Virus (1)
- dda (2)
- Indian Army (2)
- khushboo Gujarat ki (11)
- Politician (1)
- SIR Dholera (10)
- Swami Nithyananda (2)
- Women's Reservation bill's (1)
- Photo Gallery (38)
- Adalaj Vav Ahmedabad (1)
- BRTS(Janmarg) (1)
- Dudhivadar (1)
- Farmers (3)
- Gujarati Food (3)
- Idar (1)
- Indian Air Force (1)
- Lord Ganesh (1)
- Modhera sun temple (1)
- Nal Sarovar (1)
- Narendra Modi (2)
- Pavagadh & champaner (1)
- Polo Forest (1)
- Rajkot Carnival (2)
- Ran ki vav Patan (1)
- Raydi – Gujarat's Village (1)
- Royal Enfield (1)
- Sabarmati Ashram (1)
- Sardar sarovar (1)
- Sarkhej Roza Ahmedabad (1)
- Sasan Gir (4)
- Shamlaji Temple (1)
- Step well Kapadvanj (1)
- Surat City (1)
- Swarnim Gujarat (5)
- Thol Lake (1)
- sharepoint server 2010 (1)
- Student (95)
- AIEEE 2010 (3)
- Careers options after 12th (2)
- College List (23)
- HSC – Gujcet results (41)
- SSC results (27)
- Tour Packages (1)
- Uncategorized (1)
- Vibrant Gujarat (10)
- world cup 2011 (40)
- ..Online Songs / Videos (40)
Subscribe








