Friday, March 12, 2010

AJAX Webpart Integrating with SharePoint Site

Scenario: We need to get the data from SQL Server to a webpart based on the Filter conditions given by the End Users.

Pre-requisites: SharePoint Server 2007, Visual Studio 2008/2005, Must have access to SQL Server 2005 database.

Follow the below steps to create an AJAX Gridview webpart to be integrated with SharePoint Sites.


Step 1: First of all, Open Visual Studio 2008 -> New Project -> Visual C# -> SharePoint -> Webpart template. Give a name to your Project and then Click OK.

Step 2: Delete the default webpart1 that is created along with the project from Solution Explorer.

Step 3: Now right click on the Project in the Solution Explorer -> Add New Item -> Visual C# -> SharePoint -> Webpart. Give a name to your Webpart and then Click Add.


Step 4: We will be now adding some namespaces and their respective DLLs as follows.
NOTE: We require these namespaces and DLLs for the gridview and also to connect to the the Database to get the data and display it in the Gridview based on the query.
Name Spaces:
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Drawing;

DLLs:
System.Data, System.Data.SqlClient, System.Drawing and System.Web.Extensions (V1.0.6x)
NOTE: Kindly make sure that the version of System.Web.Extensions must be V1.0.6x as we will be having 2 System.Web.Extensions DLLs
Step 5: In order to get the desired webpart properties in which we have a StringConnection, SQL Query, and a Filter Name, in order to achieve this, we need to implement the following code.


a) Add an object for a Grid View Control.

GridView gv = new GridView();
b) //Deifining Controls that are to be used in our Webpart
GridView gv = new GridView();
private GridView gv;
private TextBox txtBox;
private Button btnSearch;
private Label lblMsg;
private Label lblFilterName;
private UpdatePanel updtPanel;
private UpdateProgress updtProgress;
private static readonly object EventSubmitKey = new object();
c)//Adding a variable StringConnection, Connection String, SQL Query and Filter Name

public string strConnection;
[Personalizable(), WebBrowsable(true), WebDisplayName("String Connection"), Category("Data Properties")]

public string SQLQuery;
[Personalizable(), WebBrowsable(true), WebDisplayName("SQL Query"), Category("Data Properties")]

public string filterName;
[Personalizable(), WebBrowsable(true), WebDisplayName("Filter Name"), Category("Data Properties")]

No comments:

Post a Comment

Please Leave Your Comments Here On This Post.....

Followers