Part 1: Creating and Deploying a Basic SharePoint Web Part
Tuesday, September 15th, 2009NOTE: This post is a part of the tutorial entitled Building a SharePoint List Aggregator Web Part with Custom Properties.
Creating a SharePoint Web Part
To get started, open VS 2008, click on File > New Project, choose Class Library project template, name your project, choose the location and click OK.
Close the Class1.vb file that was open by default and rename it to something more meaningful, for example ListAggregator.vb. Click Yes in the prompt window to make sure all references are renamed as well. Now open the file again.
First of all, you need to add references to the assemblies that are required for web part development – System.Web and Microsoft.SharePoint.dll. To add references, right-click on References in the Project Explorer and click Add Reference. These assemblies should be under the .NET tab. If you can’t locate Microsoft.SharePoint.dll there, get it from this directory:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI”
Include necessary namespaces and add code, as shown below:
Imports System
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages
Public Class ListAggregator
Inherits System.Web.UI.WebControls.WebParts.WebPart
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Controls.Add(New LiteralControl(“Simple Web Part”))
End Sub
End Class
Important things to note here:
- Since this is a class library, you have to create and configure all page controls in code
- All the namespaces we will need for this web part are included
- A web part has to inherit from System.Web.UI.WebControls.WebParts.WebPart class
- You need to override the CreateChildControls() method and call the base method in order to add content to the web part
- To add web controls to a web part use Controls.Add method, where Controls is a System.Web.UI.ControlCollection object
Deploying the Web Part
Before you deploy the web part you need to sign it, so that the resulting assembly is strongly named. To do this, select Project > ListAggregator Properties, click on the Signing tab, check Sign the assembly checkbox, and select New from the dropdown. Then choose a file name for the Key and click OK.
Now build the project, locate the ListAggregator.dll in the bin/Debug folder of the project directory and deploy it into the Global Assembly Cache (GAC), which is located at C:\Windows\assembly. To deploy, you need to simply drag-and-drop the file into the GAC directory. If the assembly wasn’t strongly named (signed) then you would get an error here.
Now, that your .dll is in the right place, you need to perform one more step to make this assembly “visible” to SharePoint. You need to add a SafeControl entry in the Web.config file for your SharePoint portal. This file is located in the following folder:
C:\Inetpub\wwwroot\wss\VirtualDirectories\<YourVirtualDirectoryName>
In the Web.config file, locate the closing </SafeControls> tag, and add this line right above it:
<SafeControl Assembly=“ListAggregator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=u2v507834d67109b“ Namespace=“ListAggregator“ TypeName=“*“ Safe=“True“ />
Please replace PublicKeyToken in the code below with the one for your assembly. To find out the strong name for your assembly along with some other useful information, you can use a free tool called .Net Reflector.
Now, you have to add your new web part to the Web Part gallery, which is a collection of all web parts available for use in SharePoint sites. To do this, open your SharePoint site in the browser and navigate to Home > Site Actions > Site Settings > Modify All Site Settings and click on Web Parts link under Galleries. Once there, click on the New button on the toolbar, find your assembly in the list, tick a checkbox next to it, and click the Populate Gallery button. If you can’t locate your assembly on this page, it means SharePoint cannot yet see your assembly, so double check that you performed all of the previous steps correctly.
You can also click on the Edit icon next to your web part and modify the metadata. Adding a distinctive group name will help you locate this web part faster when you add it to a page.
Adding the Web Part to a Web Part Page
Once the web part has been added to the Web Part gallery, it can be used on any of the web sites in this site collection. Just navigate to a page that you want to host this web part, click on Site Actions > Edit Page and click on Add a Web Part button in the desired web part zone. In the pop up window, check your web part and click the Add button. When done, click on Exit Edit Mode link under Site Actions. Your web part should display the text “Simple Web Part” if you completed all the steps correctly.
Continue to Part 2: Adding Custom Properties to a SharePoint Web Part.
