SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Developing a reusable SQL Data viewer Web Part for Sharepoint

Part I:   Create a basic web part in Visual Studio 2005 and deploying it to sharepoint

A few weeks back I came across a blog post that talked about a Data Viewer Web Part that can be purchased from Lightning Tools. I decided that it would be good exercise to actually develop a web part similar to the one that is offered by Lightning tools.  I have not developed sharepoint web parts in the past so it would give me some experience with sharepoint web part development and deployment. I would like to share the source code and the steps required to build a re-usable data web part that will work with SQL Server. I am posting the entire steps, code snippets and screen shots in 3 separate parts.

Before I started coding the Web Part, I had few clearly defined goals in mind

The Web Part should work on sharepoint

  1. The data source, connection and the query for the web part must be configurable through webpart properties and should not be hard coded.
  2. The user account and password for the database should be configurable from webpart properties. Once the password is entered, it must be hidden with password character.
  3. User must have the ability to enter a T-SQL command in one of the web part property and the query result should be displayed in a grid view.
  4. The grid view must have paging and user should be able to control how many records to show in each page by setting a webpart property.

Things you need before starting this project

Visual Studio 2005

  1. Experience with C# or similar programming language
  2. Sharepoint (WSS or MOSS). This project should be developed on a visual studio 2005 which is installed on the server hosting the sharepoint environment.

Building a Web Part Project in Visual Studio 2005

Start your visual studio 2005 and click:   File>> New >> Project

Click on visual C# >> Windows and select "Web Control Library"

In the Name field   enter: DataViewer and then click "OK" See Screenshot below

Visual Studio Project

 

In the solution explorer on the right, rename the file webCustomControl1.cs ( Right Click + Rename)  to DataViewer.cs

On the Dataviewer.cs delete the following lines just below the DataViewer namespace

[DefaultProperty("Text")]

[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]

  

Examine the line below

public class DataViewer : WebControl

The above line says that the DataViewer class is inheriting ( : ) from WebControl class

Since this is going to be a WebPart, we need to instead inherit from the webpart class.

Change the code to following

public class DataViewer : System.Web.UI.WebControls.WebParts.WebPart

 

In the above code, we inherited the WebPart class but typed the namespace and the entire parent class name, we can change this to  a shortened version by simply putting a reference to the parent class in the using section, type the following in the using section

 

using System.Web.UI.WebControls.WebParts;

Change the inheritance code to

public class DataViewer : WebPart

Delete the text property code. In the output.write, change it to output.Write("Data Viewer Test");

 

The final code listing should look like this

code listing 1

 

 

 

Build the project. Build>>DataViewer. This should compile without any errors.

 

On the solution Explorer, expand properties, click on assemblyinfo.cs and open the file

 

Locate the line : [assembly: AssemblyVersion("1.0.*")]

Change this to

[assembly: AssemblyVersion("1.0.0.0")]

 

The purpose of this change is to make sure that the assembly version will be the same even if we compile multiple times. For now, we just want to make sure it stays the same version, so that we will have easier time to deploy and debug the webpart.

 

Click on Project>> Dataviewer Properties

Click on the "Build" on the left side

Under "output" section locate "output path" and then click on "browse" button

In the "select output path" dialog box, select the path of a sharepoint web application and then choose the bin folder.

Example: If you have as sharepoint application under C:\inetpub\wwwroot\   then you should choose C:\inetpub\wwwroot\bin as the output path.

If you deploy the Webpart assembly to the bin folder, you can easily debug your code and do not need to provide a strong name to the assembly with a public key token.

For now, we will just deploy to the bin folder and not worry about signing the assembly with a public key token. (Note: if a bin folder does not exist, you can simply create a folder called "bin")

Build the project again, this time the dll will be output to the bin folder. There will be two new files in the bin folder called dataviewer.dll and dataviewer.pdb

The next step is to register this assembly as safe in the web.config of the sharepoint application.

Go to one folder above the bin folder and open the web.config file in notepad or visual studio 2005

 

Under the safecontrols section enter the following line

<SafeControl Assembly="DataViewer" Namespace="DataViewer" TypeName="*" Safe="true" />

The assembly name is DataViewer, the Namespace is also called DataViewer, in the typename we have a "*" which means all the classes in this assembly. Safe= true is telling the sharepoint that this webpart is safe.

Save the web.config file and close it.

Go to the sharepoint web application where you want to now add the web part

Click on Site Acttions>> Site Settings

Under "Gallery" section, click on "Web Parts"

In the webpart gallery page, click on "New" you should now see a webpart called dataviewer.dataviewer with a filename dataviewer.webpart

 

Select this webpart and click the button "Populate Gallery". This webpart is now added to the webpart gallery.

 

Click on edit icon to edit the propererties of this webpart.

 

For the group type: Data

For the Quick Add Groups type: Data

Click "OK"

web part gallery

If you got this far, the web part has been successfully deployed and added to the web part gallery. You are now ready to use this webpart in any site within the site collection.

 

Go to a site within the sharepoint site collection

Click on Site Actions>> Edit Page

On any of the web part zone, click on "Add a web part"

Scroll down to the section called "data" and select the dataviewer webpart and click "Add"

Add web part to page

 

 

The Dataviewer Webpart should now be on the page with the title "DataViewer" and the text "DataViewer Test" as shown below

web part on page

This concludes part 1 of the reusable SQL data viewer Web Part. In part 2, we will start coding a data access class. This library will have a method to execute a SQL statement and return a data table. In Part 2, we will also look at adding gridview control to our webpart.

 

 


Posted 11-30-2007 10:23 PM by sam

Comments

Paul Galvin wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 12-01-2007 7:43 AM

Awesome walk-through!

It would also by very cool to have the results load into a custom list.  If we sharepoint people could get a generic sql query to load into a custom list ... a lot of cool possibilities open up.

I wrote about using custom lists earlier this week at my blog for diving into site columns: paulgalvin.spaces.live.com/.../cns!1CC1EDB3DAA9B8AA!419.entry

Ahmed wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 12-01-2007 9:32 AM

that's a gr8 post,

don't you know how to use the MOSS extension for visual studio with VS 2008 to use the web part project template ?

sam wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 12-01-2007 1:44 PM

Ahmed , I chose not to use the MOSS extension web part project template on purpose.

I wanted to re-enforce the inheritance concept and inheriting from the webpart class instead of web control. By using the web control template, i can show this difference.

Links (12/2/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (12/2/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff
on 12-02-2007 7:06 PM

Pingback from  Links (12/2/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Sam's Blog wrote Developing a reusable SQL Data viewer WebPart for Sharepoint - Part II
on 12-02-2007 8:52 PM

Create the Data Access library and display query results in GridView This is the Part II of the 3 Parts

SHAREPOINTBlogs.com Mirror wrote Developing a reusable SQL Data viewer WebPart for Sharepoint - Part II
on 12-02-2007 8:57 PM

Create the Data Access library and display query results in GridView This is the Part II of the 3 Parts

toni wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 12-06-2007 9:23 AM

Hi

Sam ,

I am new to the programming and I need help

What I am trying to do is I want to create a form

Which must be dynamic with “VS 2005” and

Populate it MOSS and add workflow on it

Can you tell me how ….will be thankful again

Andy wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 02-17-2008 4:17 PM

Hi Sam,

Thanks for the excellent tutorial, i was able to successfully create and deploy a custom web part. However when i tried to install it on the production sharepoint server i can't see the custom web part in the new web parts list. Double checked the web.config file, assembly info, and the dll is also present in the bin directory. My guess it's related to sharepoint configuration rather than the web part..any ideas what i should verify to get the custom web part to show up??

Any help will be greatly appreciated.

Troy wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 03-28-2008 7:37 AM

Andy,  it sounds like you haven't added your new wp to the web part gallery.  Try this...

Within your site;

Go to Site Actions| Site Settings |Modify All Site Settings.  

Under the Galleries heading, select Web Parts.  Your new web part won’t be listed.  

Click New at the top and a list of all available assemblies will be displayed.  

Click the checkbox next to your new assembly and click the Populate Gallery button to make your web part available to be added to web part zones in your site.

Billy wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 04-24-2008 1:58 AM

I have followed all the steps.

But after adding the webpart to my page.

it doesnt print "DataViewer Test"

Can you please advice me what is wrong

John wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 04-27-2008 11:45 PM

Can somone please explain to me what is sharepoint application path. is it the 12 hives?

I dont know where to save the output files..

What should the output path be?

Grace wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 05-02-2008 4:50 PM

save output files the the sharepoint site bin folder

szabti wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 05-03-2008 4:11 AM

Hi All,

I passed part No1 with full success. Thanks for instructions!

So it's all fine in c#. Because I prefer VB.NET I've tried exactly the same in VB.NET Web Conrtol Library Project.  I built the project, my dll file is in the bin directory. Web.config is changed for my new DataViewerVB safe control as needed.

But when I'm going to add my DataViewVB to Web Part Gallery, it is not shown in "Web Part Gallery: New Web Parts " page (my old c# version do).

What may be wrong with VB webpart?

Loen wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 06-05-2008 1:39 PM

You may need to add the .dll to the GAC.  I had to do that and then mines worked.

JCP wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 06-11-2008 2:58 PM

this is best, most clearly written guide I've ever seen.. WOW  - Thank you so much for this..

JCP wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 06-11-2008 4:52 PM

regarding the GAC comment. In IIS if you right properties > Home directory for MOSS site/web ap you plan to use the webpart on you will find exactly where you need to place the dll.

For me it was:

C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

Stef wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 08-14-2008 9:32 AM

Finally someone who can explain this to me so that I understand every single step and it work the very first time! Thanks, well done.

Ash Chuan wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 08-21-2008 8:50 AM

Hi Sam

Your instructions are easy to follow and I am on my way to learn more from you in Part 2.

Thanks :)

DIn wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 08-27-2008 3:28 PM

I do not see the Webpart in the Gallery. When I click on New, I get "Error - File Not Found" I have copied the dll to the bin directory and added safecontrol to the web config.

Kevin wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 08-29-2008 12:10 PM

I am trying to walk through this using Visual Studio 2008(and VB instead of C#).  I have made it up to the point where I update the web.config with the SafeControl tag and refer to the namespace DataViewer and when I do that and try to hit the default.aspx page on my wss 3.0 site, it gives me a critical error.  I'm wondering if I set the namespace appropriately since it was not there to begin with and I added it into the DataViewer.vb file around the class call out.  Since sharepoint is very new to me, I'm sure I could have other issues, but everything else seemed straight forward, so I don't have much else to go on except that I'm using VS2008...

Kevin wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 08-29-2008 3:03 PM

Ok, I have fat finger syndrome and incorrectly spelled NameSpace with an uppercase S instead of Namespace in the web.config.  Now instead of the critical error, I just don't get to see the webpart in the "New Webpart" dialog.  Guess I'll have to look into adding my dll to the GAC?...

Roland wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 09-10-2008 9:23 AM

That's really a minimal, understandable solution.

Thank you very much.

If I would have tried this first instead of others descriptions I suffered with, it would have saved hours!

Kathy wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 10-08-2008 1:08 AM

I have same problem.

I do not see the Webpart in the Gallery. When I click on New, I get "Error - File Not Found" I have copied the dll to the bin directory and added safecontrol to the web config.

Any help will be greatly appreciated.

Kathy wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 10-08-2008 9:54 PM

It’s work now. I re-installed Sharepoint (WSS).

Dirk Van den Berghe SharePoint Admin Blog wrote Display SQL Data on a SharePoint site
on 10-13-2008 9:38 AM

Had this question today from my good friend Cedric. He needed a way to display non-SharePoint SQL data

DennisDeery.com wrote DennisDeery.com
on 10-14-2008 7:03 PM

Pingback from  DennisDeery.com

dataview webpart using Visual Studio WSS extensions | keyongtech wrote dataview webpart using Visual Studio WSS extensions | keyongtech
on 01-18-2009 11:29 AM

Pingback from  dataview webpart using Visual Studio WSS extensions | keyongtech

Scott wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 03-13-2009 2:59 PM

You might be able to help.  System Center Configuration Manager stores data in a SQL server.  When someone submits a helpdesk ticket with my SharePoint helpdesk, we want it to be able to query their system information for OS, RAM, different sofware loads, patches, etc. in a box for the helpdesk guys to see.  How on Earth can I do that?

hp wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 04-15-2009 3:01 PM

Hi,

I followed your steps, using VS2008 creatign a 'web part' project. then put dll into bin folder. also use gacutil put dll into gac. but still when clickc 'NEW' on web part gallery page, my new web part not listed there? any idea?

GF wrote re: Developing a reusable SQL Data viewer Web Part for Sharepoint
on 06-06-2009 4:21 AM

Where is a web control Library in VS2008 can you tell me please ?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.