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 hours of trial and error before I got it right.
The deployment method I've detailed here is very basic...there are far more professional ways to do it, but that's outside the scope of this article. I'd recommend, when using the deployment method below, to test the deployment to a test server first.
Pre-requisites
Access to a copy of Microsoft.Sharepoint.dll (you may have to extract it from the SharePoint server Global Assembly Cache (GAC)..there is lots of information on the web on how to acheive this.
Visual Studio 2008
A web server with SharePoint installed
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 the 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 file system.
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
' Display test property val;ue on the page
Me.Controls.Add(New LiteralControl("Test web part settings value = [" & TestProperty & "]<br /><br />"))
' Add any user controls to the web part here
Me.Controls.Add(New LiteralControl("Enter some text and press submit:"))
Me.Controls.Add(_txtSampleText)
Me.Controls.Add(New LiteralControl("<br />"))
Dim btn As New Button
btn.Text = "Submit"
Me.Controls.Add(New LiteralControl("<br />"))
Me.Controls.Add(btn)
Me.Controls.Add(New LiteralControl("<br />"))
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("<br />"))
Me.Controls.Add(New LiteralControl("The 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 remains enclosed in Try, Catch blocks. The same applies to any other overridable methods of the web part class. This prevents unhandled exceptions which would cause the entire SharePoint page to error.
More best practice information can be found here.
Deploying the Web Part
Prepare a Safe Control Entry for SharePoint
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.
The Web.config file can be found on the SharePoint server at:
Inetpub\wwwroot\wss\VirtualDirectories\443\
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 your 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 on the hosting server.
Populating the SharePoint web part gallery
Go to home, all site settings, Galleries, Web parts,
Click ‘New’
Tick the web part in the list (OurWebPartLibrary.TestWebPart)
Click ‘populate’
On the gallery list you edit the description, title and group. By default 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
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.
I hope this helps avoid some tears and headaches! I'll update it for SharePoint 2010 if necessary when I finally get a copy.