Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#
Article

Creating Custom Web Part

Rate me:
Please Sign up or sign in to vote.
1.63/5 (8 votes)
18 Sep 2007CPOL2 min read 45.2K   23   4
Creating Custom Web Part & integrating it with external Database using VS2005 and deploying it to Sharepoint Server

Introduction

Web part is some sort fundamental building block in a SharePoint portal. Web parts can contain information-with which user can interact, and provides personalization persistency. So a user can design the layout of his page with web parts (designed on web part zones) and it would be stored into the SharePoint content database so that user will find the layout when he/she will login next time. SharePoint ships with a bunch of built-in web parts that can add magnificent values to the development effort. I am not going to explain how we can build a web part here in this article. Because I guess Tony Rabun has already explained this in his beautiful article. I would like to add some advancement to the same i.e. interecting with a database file through our webpart.

Using the code

Without wasting any time I'm just coming to the point:

I'm taking an mdb file for demonstration.We need to add some namespaces for interacting

with the DB.

using System.Data;

using System.Data.OleDb;

After adding the Name space. We want to create a EventHandler:

For that i'll raise an event:new EventHandler(_mybutton_click);

public void _mybutton_click(object sender, EventArgs e)

{

this.Title = _mytextbox.Value;

try

{

OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=PersonDatabase.mdb");

thisConnection.Open();

OleDbDataAdapter thisAdapter = new OleDbDataAdapter("SELECT PersonID, FirstName FROM PersonTable", thisConnection);

OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);

DataSet thisDataSet = new DataSet();

thisAdapter.Fill(thisDataSet, "PersonTable");

Console.WriteLine("# rows before change: {0}", thisDataSet.Tables["PersonTable"].Rows.Count);

DataColumn[] keys = new DataColumn[1];

keys[0] = thisDataSet.Tables["PersonTable"].Columns["Au_ID"];

thisDataSet.Tables["PersonTable"].PrimaryKey = keys;

thisDataSet.Tables["PersonTable"].Columns["FirstName"].Unique = false;

 

DataRow thisRow = thisDataSet.Tables["PersonTable"].NewRow();

thisRow["PersonID"] = "17000";

thisRow["FirstName"] = _mytextbox.Value;

thisDataSet.Tables["PersonTable"].Rows.Add(thisRow);

_mylist.Items.Add(thisRow["FirstName"].ToString());

thisAdapter.Update(thisDataSet, "PersonTable");

thisConnection.Close();

}

catch

{

}

}

After creating the event handler we need to write a ChildControl which will take care of displaying the items from external database.

protected override void CreateChildControls()

{

// Create _mytextbox control.

_mytextbox = new HtmlInputText();

_mytextbox.Value = "";

 

// Create _mybutton control and wire its event handler.

_mybutton = new HtmlButton();

_mybutton.InnerText = "Set Web Part Title";

_mybutton.ServerClick += new EventHandler(_mybutton_click);

Controls.Add(_mybutton);

this.SaveControlState();

OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=PersonDatabase.mdb");

thisConnection.Open();

OleDbDataAdapter thisAdapter = new OleDbDataAdapter("SELECT PersonID, FirstName FROM PersonTable", thisConnection);

DataSet thisDataSet = new DataSet();

thisAdapter.Fill(thisDataSet, "PersonTable");

foreach (DataRow theRow in thisDataSet.Tables["PersonTable"].Rows)

{

_mylist.Items.Add(theRow["PersonID"] + "\t" + theRow["FirstName"]);

}

Controls.Add(_mylist);

Controls.Add(_mytextbox);

thisConnection.Close();

}

[Browsable(true), Category("Miscellaneous"),

DefaultValue(defaultText),

WebPartStorage(Storage.Personal),

FriendlyName("Text"), Description("Text Property")]

At the end of the code there are some attributes set to the WebPart: i.e.

Category,DefaultValue, WebPart Storage type, Friendly Name. We need to set them as shown for making it to understandable to Sharepoint Server.

Points of Interest

While developing this web part earlier I was deploying it manually to the server location i.e.

c:\Inetpub\wwwroot\wss\VirtualDirectories\80\_app_bin\. And after some work accidently I pressed F5 button in my VS2005 Id and i was surprised to see that deploying feature is integrated with the id itself.It takes care of deploy the dll to the exact location and other files to the working directory of sharepint. In my system its: C:\WINDOWS\system32\inetsrv

After Successful deployment of your Webpart add it to ur existing team site and see the efects.

Cheers!!!

Happy SharePointing....... :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
"In the attitude of silence the soul finds the path in an clearer light, and what is elusive and deceptive resolves itself into crystal clearness. My life is a long and arduous quest after knowledge & Truth."
I'm playing with all ancient and new Microsoft Technologies since last two and half years.

Comments and Discussions

 
Generalerror in convert to vs 2008 Pin
nazli20-Apr-08 22:56
nazli20-Apr-08 22:56 
this solution has error when i try open it with visual studio 2008 and it is not converted.
GeneralRe: error in convert to vs 2008 Pin
Bh@nu14-Jan-09 23:23
Bh@nu14-Jan-09 23:23 
QuestionWill this work in Windows Sharepoint services 3.0 ? Pin
anj yamsani28-Mar-08 8:15
anj yamsani28-Mar-08 8:15 
AnswerRe: Will this work in Windows Sharepoint services 3.0 ? Pin
Bh@nu20-Jul-08 23:37
Bh@nu20-Jul-08 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.