Click here to Skip to main content
15,867,330 members
Articles / Operating Systems / Windows

Deploying a Custom WebPart to Sharepoint

Rate me:
Please Sign up or sign in to vote.
4.54/5 (9 votes)
5 Mar 2009CPOL3 min read 108.9K   32   5
Deploying a Custom WebPart to Sharepoint and using it in your site

Introduction

In Part 1 of this article, I discussed how to create a custom Webpart which connects to database and pull data.

In this article (Part 2), I will show how to deploy the DatabaseConn Webpart that we have created to Sharepoint site and use it in any of your sites.

Deployment Methods

We can deploy a custom webpart in 2 ways:

  1. Deploying the Assembly (i.e. DatabaseConnWebpart.dll) to GAC
    This requires the assembly to be strong named, a change in manifest etc.
  2. Other simplest way is to deploy assembly to Sharepoint Virtual Directory folder in IIS, i.e. C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin

Note: Sometimes using way 2 may create trust problems, so if you encounter that issue, try copying your DLL to GAC too. It will solve the issue.

Copying Assembly to Sharepoint BIN Folder

Sharepoint Virtual directory can be found at: C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin.

Copy the DLL of DatabaseConnWebPart that we created from your project folder and paste it in the bin folder of portal directory and also GAC, c:\windows\assembly (if you encounter any trust issues).

Adding the Safe Control Entry

Even though the assembly is present in the Portal’s Bin folder, there is another step required to make the Control (Web Part) assembly usable on the Portal Pages.

Since the control will need to render on multiple machines in different browsers with as many user accounts as the organizations have, there is a need to declare the control as “safe”.

To do so, open the web.config file of Sharepoint portal placed under the portal’s directory, and look for <SafeControls> node in web.config and add the following Safe control code:

XML
// Check for this node

<SafeControls>
………..
………....

// Add this line of code which ensures webpart is safe

<SafeControl Assembly="DatabaseConnWebPart " Namespace="DatabaseConnWebPart "
    TypeName="*" Safe="True" />

</SafeControls>

Now that we declared the webpart as safe, our webpart will be deployed to Sharepoint Portal.

Configuring Portal to Use NewWebPart

Open your Sharepoint portal and on your “Home”, click Site Actions -> Site Settings -> Modify all Site Settings as shown in this figure:

SiteSettings_small.jpg

Sharepoint will open the Settings page.

Click “Webparts” under Galleries as shown below:

webparts_small.jpg

Adding Custom Webpart to Webparts Gallery

Click “New” in toolbar to add a new webpart as shown below:

WebPartGallery_small.jpg

Sharepoint will open the webparts gallery, choose your DatabaseConnWebPart from the gallery and check the checkbox to the left of webpart and click Populate Gallery on top as shown below.

Note: You can rename your webpart anyway you want. Use the TextBox provided to the right of your webpart to rename it accordingly.

NewWebPart_small.jpg

Now we have successfully added our new custom web part to Sharepoint portal. We can go ahead and use that webpart in any site that we want.

Note : If you want to make sure whether your webpart is added to gallery or not, check the Webpart Gallery page. It should have your webpart.

Using DatabaseConnWebPart in Site

To use our new deployed webpart in a site, open the site and Click Site Actions -> Edit Page.

This will show the site in edit mode, now click "Add a WebPart" as shown below:

AddwebPart_small.jpg

This will open the Add WebPart dialog box. Choose your webpart (in Miscellaneous section) and check it and click "Add" as shown below:

Add_webparts_-WebPage_Dialog.jpg

This will add the webpart to your home page and now your homepage as shown below:

HomepageWithWP_small.jpg

Conclusion

This is how we can create custom webparts, deploy them to Sharepoint server and use them in our sites.

Happy programming!!!

History

  • 5th March, 2009: Initial post

License

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


Written By
Software Developer (Senior) Monster
United States United States
A .Net/Sharepoint Developer currently working with MONSTER in Boston, MA.

I like coding especially with C#,jquery, xml, xslt.

My hobbies include painting, poetry, photography and long drives.

Comments and Discussions

 
GeneralMisssing steps Pin
shatrunjay shukla20-Mar-12 9:04
professionalshatrunjay shukla20-Mar-12 9:04 
GeneralA error occured while previewing a webpart. Pin
patelsamir25210-Mar-11 6:09
patelsamir25210-Mar-11 6:09 
Generalsharepoint module problem Pin Pin
saiprakashsai19-Apr-10 20:43
saiprakashsai19-Apr-10 20:43 
QuestionCustomize webpart for sharepoint Pin
serenadee20-Apr-09 19:50
serenadee20-Apr-09 19:50 
GeneralDeploying a Custom WebPart to Sharepoint Pin
xusan19-Mar-09 4:30
xusan19-Mar-09 4:30 

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.