asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Building an ASP.NET 2.0 Web Part for Deployment to WSS V3.0 and MOSS 2007 - Part 1
By Gayan Peiris
Rating: 3.4 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article



  • 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.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs