How to create and deploy a basic SharePoint web part with VB.NET
There are many blogs and posts on how to do this, but I found most of the were incomplete, or missed out vital information. It took me ages of trial and error before I got it right, so here goes:
Pre-requisites
Access to a copy of Microsoft.Sharepoint.dll
Visual Studio 2008
Initial Setup
Open Visual Studio 2008.
Go to New -> Project, and select a Windows Class Library.
Name the project 'OurWebpartLibrary'
Delete the Class.vb file.
Setting up the Properties Page
Project References & Signing:
Using the solution explorer, right-click the uppermost node, and select ‘Properties’
Click ‘References’, Click ‘add’, then browse to, and add a reference to Microsoft.SharePoint.dll
Next, click ‘Add’ again, and add the following .NET reference: System. Web. (you won't have to browse for this, use the .NET tab)
Click ‘Signing’, tick ‘Sign the assembly’, and select <New..>, name the keyfile and set the password
Build Path
Click on the ‘Compile’ tab and choose the Release configuration.
Change the build output path to ‘bin\’ and click ‘Browse’. Create a folder called ‘Release’, click on it, then click ‘Select Folder’.
Now click ‘Save All’, and save the project to your filesystem.
Assembly Security
Right-Click the root node in the Solution Explorer and select ‘Open Folder in windows explorer’.
Browse to the ‘My Project’ folder
Double-click ‘AssemblyInfo.vb’
Copy and paste this into the Imports section at the top of the file:
Imports System.Security
Paste the following line into the Assembly Attributes: <Assembly: AllowPartiallyTrustedCallers()>
Click ‘Save All’
Now to create a web part!
Right-Click the root node in the Solution Explorer and select 'Add', then 'New Item'
Select 'Class', name it 'TestWebPart.vb', clear all the code in the file and paste this in:
Option Strict On
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Public Class TestWebPart : Inherits Microsoft.SharePoint.WebPartPages.WebPart
' Declare any user controls here e.g.
Private _txtSampleText As New TextBox
' Declare any property variables here
Private m_TestProperty As String
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Try
' Add any user controls to the web part here
Me.Controls.Add(New LiteralControl(TestProperty & "<br />"))
Me.Controls.Add(_txtSampleText)
Me.Controls.Add(New LiteralControl("<br />"))
Dim btn As New Button
btn.Text = "Submit"
Me.Controls.Add(btn)
Catch err As Exception
Me.Controls.Add(New LiteralControl("CreateChildControls error: " & err.Message.ToString))
End Try
End Sub
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
Try
' The values the user has input are available during OnPreRender
Me.Controls.Add(New LiteralControl("OnPreRender: Sample Text is [" & _txtSampleText.Text & "]<br />"))
Catch err As Exception
Me.Controls.Add(New LiteralControl("OnPreRender error: " & err.Message.ToString))
End Try
End Sub
<WebBrowsable(True), Personalizable(True), WebDisplayName("Test Property"), ComponentModel.Category("Custom Settings")> _
Public Property TestProperty() As String
Get
Return m_TestProperty
End Get
Set(ByVal value As String)
m_TestProperty = value
End Set
End Property
End Class
4) Now Build the project, there should be no build errors.
It's imperative that code inserted into the overridden methods remain enclosed in Try, Catch blocks. The same applies to any other overridable methods of the web part class. This prevents an unhandled exception causing the entire SharePoint page to error.
Deploying the Web Part
Prepare the Safe Control Entry
Copy the following line and paste it into a text editor.
<SafeControl Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXX" Namespace="RootNameSpaceName" TypeName="*" Safe="True" />
Using the solution explorer, right-click the uppermost node, and select ‘Properties’, then Click 'Application'
Update the AssemblyName attribute and Namespace in the text editor. Next, for some reason a tricky bit,
you need to find the Public Key Token of your OurWebPartLibrary.dll and put it in the SafeControl entry.
You can find your OurWebPartLibrary.dll by right-clicking the solution explorer uppermost node, selecting 'Open Folder in Windows Explorer' and going to 'Bin' then 'Release'
The public key token can be found easily by using a .NET reflector tool, or by following the MSDN instructions.
Register the Web Part as a Safe Control with SharePoint
1) Use Visual Studio to open the web.config file and insert the edited line into the <SafeControls> area. Insert it underneath any existing safe controls registered there. If VS is not available, use a text editor that can remember multiple changes which can be undone. e.g. NOT notepad.
2) Save the web.config file and test that the server is still serving pages ok. There may be a hang of approximately 10 seconds while it deals with the change. If there is an error, immediately hold Ctrl and Z in yoru editor until all changes are undone, then press save.
3) When the SafeControl entry is configured and the SharePOint server is happily serving pages, make sure you close the web.config file immediately to prevent any accidents.
Installing the DLL on SharePoint
Drop the OurWebPartLibrary.dll file found in the '\Bin\Release folder into Inetpub\wwwroot\wss\VirtualDirectories\443\bin.
Populating the SharePoint web part gallery
Go to home, all site settings, Galleries, Web parts,
Click ‘New’
Tick the web part in the list
Click ‘populate’
On the gallery list you edit the description, title and group. By defalt the WebPart will appear in the 'Miscellaneous' group.
Adding further web parts
As long as any subsequent web parts are defined in the same way e.g without specifying a Name Space for the web part, you 'll only need to do this:
- Insert a new Webpart class file to the project
- Build the project
- re-install the DLL on SharePoint (ignoring the safecontrols part)
- Populate SharePoint gallery
- Populating the SharePoint web part gallery
If you start creating web parts under different namespaces they will need a SafeControl entry allowing that namespace before you'll see them in the Gallery.

0 Comments
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now