Date: 29 Jun 2017
Welcome, Guest
Complete, all you can do here.
Sign Up »
Sonicly

Sonicly Menu

Main » 2010 » December » 25 » How to make a Simple Web Part for MOSS/WSS in VB.net
10.39
How to make a Simple Web Part for MOSS/WSS in VB.net
Added by: mukakisut
Saturday, 25 Dec 2010, 10.39

So here is my howto based on the simplest way I have found to start playing int he wonderfull world of WebParts

Writing the Web Part
At this point I am going to make some assumptions.

1. You have a MOSS/WSS installation up and running.
2. You have Visual Studio 2008 VB.net installed

Now that the assumptions are out of the way we can get down to business.

1. Open Visual Studio 2008
2. Choose File | New | Project
3. Under Visual Basic pick Windows and select Class Library from the templates. Under Name call it "HelloWorld" and press OK
4. Since all we are going to do is display some information we don't need lots of references, but we do need to add the System.Web namespace. To do this we will select "Add Reference" from the Project menu and pick "system.web" under the .NET tab and press OK.
5. In the Solution Explorer window on the right we will rename Class1.vb to HelloWorld.vb so that it is more descriptive. You should notes that it renames the class name inside the file as well.
6.
To make things easier we will add the following import section to the top of the file above the Public Class statement.
Imports System.Web.UI.WebControls.WebParts

1.
We will also make our class inherit from the web part class by changing the line "Public Class HelloWorld" to read
Public Class HelloWorld : Inherits WebPart

1. Underneath the class name we just edited we will override the RenderContents method with our own output. So type out "Overrides RenderContents" and press TAB to let the smart tags do the work. You should now have a line that looks like this:

Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.RenderContents(writer)
End Sub

1.
Now we will replace the generated code "MyBase.RenderContents(writer) " with our own output and make the line look like:
writer.write("Hello World!")

At this point our code page should look like the output below.
Imports System.Web.UI.WebControls.WebParts
Public Class Hello : Inherits WebPart
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
writer.Write("Hello World!")
End Sub
End Class
Deploying the Web part
There are numerous ways to deploy a web part. We can use the GAC and strong names, but I want this to be as easy as possible so we are just going to deploy directly to our website.

1. Select Project and choose "HelloWorld" Properties
2. Under the Compile tab change the Build Output directory to the bin folder of your website. In my case I moved it to \\server\c$\Inetpub\wwwroot\wss\VirtualDirectories\80\bin
3. To build the webpart choose "Build HelloWorld" from the Build menu. You should see the HelloWorld.dll in the bin directory under the site along with a pdb and xml file.
4. Now we need to add the webpart to the save controls list for the site. Open up the web.config file and look for the "" tag that indicates the end of the Save Controls section.
5.
You will notice that all of the Microsoft lines use a strong name, public keys, versions etc. This is more secure, but not necessary. In our case we will just add our assembly name and an * to designate we are exposing all methods (I would not do this in a production environment, but it is ok for my test environment and will allow for quick deployment). Our line will look like
<SafeControl Assembly="HelloWorld" Namespace="HelloWorld" TypeName="*" />

1. Save the web.config. The act of saving the file is enough to restart the application so the site should recycle and get our changes

Testing the Web Part
At this point the web part is deployed, but not enabled. To use the web part we have to do the following:

1. Open the Website and browse to the Site Settings page
2. Under Galleries choose "Web Parts"
3. Click New and you should see your web part listed as HelloWorld.Hello or Hello.webpart. Check the box and click the "Populate Gallery button"

You should now be able to browse to the home page of the site and add our new Hello web part to the page from All Web Parts -> Miscellaneous. Cool huh?
Changing the Code
But wait, there is more! Let's say we want to get fancy.

1.
Change the output line. For example.
writer.write("Hello " & Context.User.Identy.Name)
2. Then build your class again. (Build | Build HelloWorld)
3. Go back to your webpage and hit refresh

Without having to re-deploy your web part has been updated!
This is the easiest way I have found to write SharePoint web parts. It cuts out all the bloat from the templates and allows for lighting fast testing.

Views: 103 | Rating: 0.0/0
Total Comments: 0
Only registered users can add comments.
[ Register | Login ]

Sign In

E-mail:
Password:

Sonicly Totalistic

Member: 5095
Forum: 2322
News: 27
Blog: 16
Downloads: 18
Comments: 5

Sonicly Statistic

Total Online: 1
Guests: 1
Users: 0