Building a Hello World Web Part for Windows SharePoint Services 3.0
page 5 of 9
by Steven Barden
Feedback
Average Rating: 
Views (Total / Last 10 Days): 38311/ 81

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);
        }
    }
}

View Entire Article

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-23 4:22:28 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search