|
download source code
The next version of SharePoint supports both the ASP.NET 2.0 web part framework and the standard
SharePoint web part framework. Sometimes this can be confusing for the developers understanding
the difference between the two frameworks and when to use what. I have explained both the
frameworks and their differences in my previous article on
Building Web Parts for Windows SharePoint Services 3.0 (ASP.NET 2.0 / WSS V3.0)
In this article, you will be looking at how to create an ASP.NET 2.0 web part using Visual Studio 2005 and C# language. You will create a simple ASP.NET 2.0 web part that will display the logged in user's name. You will learn how to upload the web part to the web part gallery and, finally, to add the ASP.NET 2.0 web part to the Windows SharePoint Services (WSS) V3.0 site page.
In this example, Visual Studio 2005, Microsoft Office SharePoint Server (MOSS) 2007 and WSS V3.0 are all installed in a single development machine.
Create an ASP.NET Web Part in Visual Studio 2005
First, open Visual Studio 2005.
I will be using a normal Class Library project to create the ASP.NET web part.
Click File -> New Project.
Select Visual C# in the Project Type window.
Then select Class Library in the Templates window.
Type ASPNETWebPartHello for the name.
Select your preferred location to save the project on the Location.
When finished, click OK.

Figure 1: New Project window
Add Reference to the System.Web DLL
The next step is to add a web reference to the System.Web.dll.
Navigate to the Solution Explorer window,
right-click on References, then select Add Reference option

Figure 2: Selecting Add Reference option in the Solution Explorer window
Click the .NET tab and select System.Web as displayed in Figure 3

Figure 3: Selecting the System.Web DLL from the list
The System.Web DLL will be added to the reference list as displayed in figure 4

Figure 4: System.Web DLL in the reference list
Add Required Namespaces to the Class
Add the following namespaces to the class
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
Inherit the Class from System.Web.UI.WebControls.WebPart
Rename the Class1.cs file to Hello.cs. You must inherit the Hello class from the System.Web.UI.WebControls.WebParts to create an ASP.NET Web Part.
public class Hello: WebPart
Add Code to the Web Part Class
You must overwrite the RenderContents method as displayed below.
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("Hi " + this.Context.User.Identity.Name);
}
The RenderContents method renders the contents of the control to the specified writer. The writer parameter is an HtmlTextWriter that represents the output stream used to render HTML content on the client. If your control has child controls, you must either call the base RenderContents method or call the RenderChildren at the point where you want the child controls to be rendered to the text writer.
The final Hello web part class will be displayed as in figure 5

Figure 5: Hello web part class
Build the Web Part
The next thing to do is to build the application.
Before you build the application, let's change the build path to the bin directory of the SharePoint site. Otherwise you will have to manually copy and paste the ASPNETWebPartHello.dll to the SharePoint BIN directory. By default, there won't be any bin directory on the physical directory of your SharePoint site. You will have to manually create the bin directory for the first time.
You have the option of deploying the DLL file to the Global Assembly Cache (GAC) or the BIN directory of the SharePoint physical directory. You should use GAC in a production environment when the DLL is strong named. I will use the bin directory, because it will make the developer's life much easier to debug and test the solution.
Right click on ASPNETWebPartHello solution and select the Properties option.
This will display the Assembly name and Default namespace options in the Application tab.

Figure 6: Application tab in the properties window
Click the Build tab and select Output path. Click the Browser button and navigate to the SharePoint bin directory as displayed in Figure 7.

Figure 7: Setting up the output build path to the SharePoint bin directory.
Now you can build the application!
Add Safe Control entry
You must add an entry about your web part in the Safe Control list in the SharePoint web.config file. The web parts that are listed in Safe Control list are allowed to be executed in the SharePoint environment. A standard safe control list entry looks like following line:
<SafeControl Assembly="[Assembly Name]" Namespace="[Namespace]" TypeName="*" Safe="True" />
You can enter the Class name of the web part as the TypeName. Entering "*" in the TypeName section will allow all the web parts that are created under the same Assembly and Namespace as safe controls.
Open the web.config file and search for the <SafeControls> element. Add the following entry to add your web part to the end of safe control list
<SafeControls>
Other web part entries...
<SafeControl Assembly="ASPNETWebPartHello" Namespace=" ASPNETWebPartHello " TypeName="*" Safe="True" />
</SafeControls>
Continue to Part 2 ->
About the Author
Gayan Peiris (MVP, MCSD, MCAD, MIT, BComp) is an expert in architecting and designing SharePoint
Portals and Windows SharePoint Services solutions. He has written many articles, reviews and columns for various
online publications. Gayan is a frequent speaker at Microsoft developer conferences on SharePoint technology.
He has been a leading SharePoint advocate since 2001 and is president of
the Canberra SharePoint User Group.
Gayan been presented with
MVP award for Microsoft Office SharePoint Portal Server.
Gayan works as a Technical Architect/Principal Consultant for Unique World Pty Ltd
in Canberra, Australia. He can be reached at
http://www.gayanpeiris.com and
http://gpeiris.blogspot.com.
|