Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Creating Custom Web Part

1.63/5 (8 votes)
18 Sep 2007CPOL2 min read 1   255  
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)