Wednesday, June 24, 2009

The two ways of developing custom webparts using .Net

Sharepoint comes up with some out-of-the box (OOB) webparts that can be used in sharepoint applications. In addition sharepoint is flexible enough for custom webpoarts. We can create custom webparts using .net and these webparts can be used in sharepoint pages.

There are two ways to create custom webpart-
1. using User control
2. using class library project

One may ask that which method is better. As far as I am concern, when you need a complex UI in your webpart then use user control otherwise you may use class library project.

Creating webpart using User control

We need to follow few steps-

1. Create an ASP.Net web application and add an user control into it. Give some interesting name to it.
2. create your UI and write your code and build your application.
You can deploy it in two way-
a. Using smartpart- a third party tool to add user control to your sharepoint site
b. by deploying the DLL into GAC and putting your ASCX file into
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory.

Smartpart can be downloaded from here

Deployment using smartpart
  • Take the DLL and put it in the bin folder of your web application. i.e. inside C:\Inetpub\wwwroot\wss\VirtualDirectories\port number\BIN\ directory.

  • Create a folder named 'UserControls' inside the web application ( inside the C:\Inetpub\wwwroot\wss\VirtualDirectories\port number\ directory) and put the ascx file in that folder.

  • Open the web.config. Increase the trust level to Full. we dont need safecontrol entry for user control as it will be there for smartpart.

  • Do an IIS reset. your user control is ready to use.

  • To use it, Open the page where you want to add webpart, in edit mode. Click on 'add a webpart' in webpart zone. select smartpart from webpart zone. open the tool pane and select the user control. click on Ok. Its done now.

Deployment without Smartpart

  • Sign your web Application with strong name and build it again.

  • take your DLL and put it in GAC.

  • Take the ASCX file and put it on C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory.

  • Add a SafeControls entry into web.config.

  • Do an IIS reset.

  • Go to webpart gallery on your site and click on New tab.

  • Select your webpart and populate the gallery with it. Now the webpart will be in webpart gallery for use.


Creating webpart using class library project

Follow the below steps-
  • Open visual studio and create class library project.

  • write your custom code for webparts and rendering controls using CreatChildControls() and Render() methods.

  • Sign your project with strong name.

  • Build the project.

  • Put the DLL into the GAC.

  • Add the SafeControl entry into the web.config file specifying the namespace and assembly.

  • Do an IIS reset.

  • Go to webpart gallery on your site and click on New tab.

  • Select your webpart and populate the gallery with it. Now the webpart will be in webpart gallery for use.


Thats It.
Hopes it would be helpful for everyone.

Regards
Deewaker

No comments:

Post a Comment