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

Developing Web Parts for MOSS is easy!

5.00/5 (1 vote)
23 Mar 2010CPOL2 min read 1  
In this post, I will show you how easy it is to develop webparts in MOSS (Sharepoint). If you know how to develop in .NET, then it will be a breeze. It will be as easy as 13 steps, so here we go.

In this post, I will show you how easy it is to develop webparts in MOSS (Sharepoint). If you know how to develop in .NET, then it will be a breeze. It will be as easy as 13 steps, so here we go.

Step 1

Create a new Class Library Project and name it any way you want. I named mine as MyWebPart.

Class Library Project

Step 2

You now add a reference to System.Web as you are using the following namespaces:

C#
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

You can do that by right clicking on the project and choose add a reference, choose the .NET tab:

Add Reference

Do your coding now. Here is a sample code on how to render controls on the webpart.

C#
namespace MyWebPart
{
 public class NewWebPart : WebPart
 {
 protected override void CreateChildControls()
 {
 TextBox myTextBox = new TextBox();
 myTextBox.Text = "This is easy";
 Controls.Add(myTextBox);
 }
 public override void RenderControl(HtmlTextWriter writer)
 {
 RenderChildren(writer);
 }
 }
}

In this sample, it's just a simple rendering of a TextBox called myTextBox and assigning a value to the textbox. Once done, you are ready to publish the code to your MOSS.

Step 3

Determine the bin folder of your MOSS site, you can find it in IIS. Usually it's in C:\Inetpub\wwwroot\wss\VirtualDirectories\{your MOSS instance name}\bin:

MOSS bin folder

Step 4

Once found, copy and put that in your output path in the build properties of your project, also choose All configurations in the Configuration Section, so when you debug, it publishes on your MOSS bin folder (not a best practice but we just doing it for simplicity of this walkthrough):

Output Path build properties

Step 5

Build the project and you will now see the DLL on the MOSS bin folder:

MOSS Bin Folder

Step 6

Add a SafeControl Assembly on the web.config of your MOSS instance. That would be one directory above the bin folder.

Safe Control Assembly

Step 7

Now that your webpart is on the server, you need MOSS to import the library you created for you. You can do that by going to Site Settings and Modify Site Settings:

Site Actions

Step 8

Now go to Galleries and choose Web Parts:

Web Part Galleries

Step 9

Add the WebPart by clicking New:

New Web Part

Step 10

Choose the webpart you just developed by ticking the checkbox beside it and click Populate Gallery:

Populate Gallery

Step 11

Now your new webpart is available for use in any of your pages and it is now included in the library:

New WebPart

Step 12

Now you can use it in any page. If you add a new webpart, it will be on the list:

Web Part List

Step 13

Now you can use it. It's that simple!

Finished Product

License

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