Building a Hello World Web Part for Windows SharePoint Services 3.0
 
Published: 29 Oct 2007
Abstract
This article examines how to develop a simple Hello World Web Part using Windows SharePoint Services 3.0 with the help of a series of steps.
by Steven Barden
Feedback
Average Rating: 
Views (Total / Last 10 Days): 38324/ 66

Introduction

The purpose of this article is to demonstrate how to develop a very simple Hello World Web Part. The target application for this Web Part is Windows SharePoint services 3.0 (WSS), although it could of course be installed on Microsoft Office SharePoint Server (MOSS). The development of this assembly will be conducted on a system that does not have WSS/MOSS installed. The conclusion will demonstrate how to manually deploy and run this code, as opposed to using Visual Studio's built in deploy process.

This article is inspired by the work of Ted Pattison, whose video this writing is based upon and loosely follows.

Assumptions

1.    You are running or have access to an instance of Windows Server 2003 running (at minimum) WSS 3.0 or MOSS

2.    You have installed Visual Studio 2005. This may be on a workstation separate of your WSS/ MOSS installation.

Steps

We will perform the following action, broken into document parts:

3.    Build a Windows SharePoint 3.0 Website.

4.    Build an ASP.NET Web Part.

5.    Deploy the WebPart by compiling it to the bin directory of the SharePoint site.

6.    Add a safe-control entry in to the Web.config.

7.    Modify the Web Part Gallery to display the Web Part for testing and debugging.

Part 1: Building The SharePoint 3.0 Website

1.    Start by opening the Administration tools via the Start Button and navigate to SharePoint 3.0 Central Administration.

2.    Open the Application Management page and select Create Site Collection under the SharePoint Site Management block.

3.    Using the next page displayed, create your demonstration site.

4.    Create a title, this can be anything you want, such as "DevSite."

5.    Fill in the Url text box. This could also be "DevSite."

6.    Add a user account as the site owner. This will likely be the account you are currently logged in as.

7.    Select a site template. The easiest choice for this demonstration can be Blank Site.

8.    SharePoint will then provision and build your new site.

9.    Use the link provided at the end of the provisioning process to open your new website.

Part 2: Developing The Web Part

1.    Open Visual Studio 2005 and start a new C# class library.

2.    Add a reference to System.Web.

3.    Add the following using statements at the top of your file.

Listing 1

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

4.    Rename your class to Hello by renaming the class file, which should rename that class name inside the file.

5.     Change the class to inherit from Web Part.

6.     Inside the class, override the base class RenderContents method.

7.     Replace the automatically added line referring to the base rendercontents method with your own content.

8.     To show the fact that you are programming in the ASP.NET model you may use one of the built in objects from the library.

9.     Below is an example of what the final product may look like.

Listing 2

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
namespace HelloWorldWebPart
{
    public class Hello : WebPart
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(@"Hi, " + this.Context.User.Identity.Name);
        }
    }
}
Part 3: Deploying The Manual Way

There are multiple ways that you can deploy this Web Part; for example, you could add a strong name and deploy to the GAC. Or you can deploy it to the bin directory of the website you are using, which is the option this article uses.

1.    Right mouse the project in the solution explorer.

2.    Select properties.

3.    Select the Build tab.

4.    Change the output path to the bin directory of the site you are deploying to. If the bin directory does not exist you can create a new one. As indicated previously, we do not need to develop this webpart on the development server. So one example you can use as your output path is seen below. The good part of this means you can develop simple webparts on any version of Windows using Visual Studio 2005, then deploy to your intended location:

\\<servername>\c$\Inetpub\<sharepoint-root>\bin.

5.    Now build your project using <Control><Shift><B> or from the menu options.

6.    Now you need to build a safe entry in the Web.config. Open the Web.config file with Visual Studio.

7.    Locate a safe control entry, such as the line with Microsoft.Office.Excel.WebUI, and make a copy below the original line.

8.    Notice that all of Microsoft’s lines use a four part strong name. This example does not need a four part name since we are not using strong names. You can use just the Assembly name without the .dll extension, in this case "HelloWorldWebPart."

9.     Next, replace the namespace, in this case "HelloWorldWebPart."

10.  Finally, fill in the type name. You can add any of the methods you have written that you want to deem safe to expose or use "*" for all methods.

Listing 3

<SafeControl Assembly="HelloWorldWebPart" 
    Namespace="HelloWorldWebPart" TypeName="*" />

11. The act of saving the Web.config file is enough to reset the application, so there is no need to perform an iisreset. The site will automatically recycle to get the changes.

Part 4: Testing The New WebPart

1.    Open your website. At this point your new Web Part is not yet an option to be selected and must be enabled for use.

2.    Open the Site Settings.

3.    Under Galleries click the Web Parts link.

4.    Web Parts are controlled by XML files. But in this case we did not craft an xml file for this webpart. The act of enabling this webpart for use will assist with this need.

5.    Click New, which causes SharePoint to read the Web.config file. It reads the config and uses reflection to build the information needed to allow you to use the webpart.

6.    You should now see the Hello webpart. Click the Populate Webpart option.

7.    Now back in the main Web Part Gallery page, take a minute to review the webpart properties by clicking on the Edit link button. Click on the View Xml link. You may find this bare-bones config file useful in other development tasks.

8.    Now go to the main page. Click on Site Settings, and edit page. In the middle, click on Add A Web Part and locate the Hello webpart.

9.    Your webpart should now be displayed.

Part 4: Changing The Code

1.    Finally, we will change the code to update the webpart output, without re-deploying.

2.    Go back into your code and change the output line; for example, what is listed below.

Listing 4

writer.Write(@"Hi there, " + this.Context.User.Identity.Name + 
    @" (this output has been changed and recompiled.)");

3.    Press <Control><Shift><B> to rebuild your assembly.

4.    Go back to your web page and press refresh.

5.    Without having to re-deploy, your Web Part's output has been updated.

6.    This now becomes a fast way to iterate through the process of building, deploying, testing, updating and testing again your code.

Conclusion

In this article you learned how to develop a very simple Hello World Web Part using Windows SharePoint Services 3.0 in a step-by-step way.



User Comments

Title: There is no Web.confg file   
Name: Steven Barden
Date: 2010-03-05 6:05:31 AM
Comment:
The reason there is no Web.config is because this project is not a web project. It is a web-based component that will be added into SharePoint, which is a web-application and will have it's own web.config. That said, you will have to register your web componenet with the web you want to show it from... actually with the site collection. (site collections and webs are another topic). So think of your web-part as being a specialized user-conrol. The user-control can be developed alone, then added to a web-project. That is the case here as well. I hope this helps.
Title: There is no Web.confg file   
Name: HelloWorld
Date: 2010-03-05 5:57:16 AM
Comment:
There is no Web.config file in my VS.
What to do next?
Title: Great   
Name: GRF
Date: 2009-09-03 4:43:03 AM
Comment:
Your tutorial opens me a new world!
Title: Outstanding   
Name: kp
Date: 2009-03-03 6:44:40 AM
Comment:
Really clear and good
Title: Outstanding!   
Name: kp
Date: 2008-11-17 5:12:53 AM
Comment:
Really clear and good
Title: really good   
Name: Tom Morris
Date: 2008-09-23 5:56:39 AM
Comment:
Really good introducing!thx
Title: AWESOME   
Name: M.Czarnecki
Date: 2007-11-02 2:43:03 PM
Comment:
Absolutely fantastic.
You really reamoved all the bloat and the ridiculous hoops that MS wants us to go through. I don't have time for all that garbage. Your tutorial is a gem.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 4:30:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search