Posts Tagged ‘web part’

Part 1: Creating and Deploying a Basic SharePoint Web Part

Tuesday, September 15th, 2009

NOTE: 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.

Building a SharePoint List Aggregator Web Part with Custom Properties

Tuesday, September 15th, 2009

This is a tutorial on how to build a customizable web part for MOSS 2007 or WSS 3.0.

We will be building a List Aggregator – a web part that can collect, filter, and display list items from all lists of a certain type from all sub-sites in up to 3 site collections. This web part will have the following features:

  • Users will be able to select up to three site collections to aggregate content from
  • Users will be able to choose one of these list types: announcements, calendar events, or tasks
  • Only current announcements and calendar events will be displayed
  • Only tasks that are in “Not Started”, “In Progress”, or “Waiting on Someone Else” will be displayed

This web part is somewhat similar to the out-of-the-box Content Query web part, but Content Query can only aggregate content from a single site collection. On the other hand, List Aggregator will not have the robust filtering that Content Query provides.

First, we will build a basic web part and deploy it. Then, we will add some custom properties to enable users to change web part settings in the UI. At last, we will add functionality to collect list items from multiple SharePoint lists. Hmm, not quite the “Hello World” web part you expected, is it?

To successfully complete this tutorial, make sure you have access to either a physical or virtual development environment that has Windows Server 2003 and either WSS 3.0 or MOSS 2007. Also, make sure it has Visual Studio 2008 installed. We will be using VB.NET.

The rest of this tutorial is broken into three parts, which I will add as they become available. Click on one of the links below to continue:

  1. Creating and Deploying a Basic SharePoint Web Part
  2. Adding Custom Properties to a SharePoint Web Part
  3. Adding CAML Queries to a Web Part to Collect Data from SharePoint Lists