We will start from the Visual Studio 2005 new project window. In the new project window, we will select C# Window application and then, class library. We will name the project “HELLOBIN”.
The first thing we will do in the class library is to add reference of the name space System.Web. This is done by right clicking on the References link in the solution explorer and then clicking on “Add Reference…”.
Clicking on add reference will generate a new pop up window Add Reference. We will select the System.Web namespace, as encircled in the figure below, and click on OK. It will add the System.Web namespace in our project and details of which can be seen in the Reference link.
We will add the names spaces, encircled red in the figure below, in our project so that we can directly use the classes in that namespace without specifying the complete path.
We will again go to the solution explorer and change the name our class from “Class1” to “HELLOBINCLASS”. This can be done by clicking on the class name in the solution explorer and right click on the class name. This will open a new window from which we will select Rename and change the class name from “Class1” to “HELLOBINCLASS”. As you change the name of the class, a prompt will appear, shown in the figure below, and ask would you like to rename all references to the code element “Class1”? Click Yes.
We will inherit our class from the class WebPart. The path to the class WebPart is System.Web.UI.WebControls.WebParts.WebPArt. We have already added the namespace System.Web.UI.WebControls.WebParts; therefore, we just right the name of the class WebPart for inheritance.
We will create a simple program for demonstration purpose. The code will override a method Render. The exact code of the method is shown in the figure below.
After writing the code, we will change the out put path setting of the project. For this, we will right click on the project name in the solution explorer and then, we will click on the properties.
On clicking the properties, a new window will be appeared. We will select the build pane of the window as shown in the figure below. In the build pane, we will set the out path to the bin directory of the MOSS. In our case, the path is C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin which is encircled in the figure below:
We will build by pressing F6 or clicking on Build in the tolls menu and then clicking on Build Solution. Build will generate the file HELLOBIN.dll in the bin directory.
We have to add the reference to the web.config. The file will be located in the folder created by MOSS. In our case, the path of the file is C:\Inetpub\wwwroot\wss\VirtualDirectories\80. We will open the file in the Visual Studio and go to the tag <SafeControls>. Here we will add a new entry for our web part in the <SafeControls> tag which is <SafeControl Assembly=”HELLOBIN” Namespace=”HELLOBIN” TypeName=”*” Safe=”True” />, encircled in the figure below:
After writing the entry, don’t forget to save the web.config file. Becareful while making changes in the web.config file because if this file get corrupted, i am afraid you will be in big trouble. So workaround is that save a copy of file and then make changes so that you have orginal copy of the file intact.
Now we demonstrate the next phase of adding the web part in the sharepoint site. For this, click on the “Site Action” of your sharepoint main page and click on “Modify All Site Settings” by moving through “Site Settings”.
Site Settings page will be opened. Click on the link “Web Parts” under “Galleries”.
Web Part Gallery web page will be opened. Click on the “New” to add the web part in the gallery. Clicking on “New” will bring you the list of all the web parts and our web part HELLOCINCLASS will be available in the list. Select the Web Part “HELLOBIN.HELLOCINCLASS” (Name of the namespace followed by the name of the class) and click on the button “Populate Gallery” as shown in the figure below:
After adding the web part in the web part gallery, you can easily add the web part on the desired site. For this, click on the Site Action and then on Edit Page. The page will be open in the edit mode. Click on the link “Add a Web Part” in the space where you want to add the web part.
Clicking on “Add a Web Part” will result in a new window which will contain the list of all the web parts. From the list, select the web part you have created.
After adding the web part, click on the “Exit Edit Mode” and the web part will be working on the site.