none
Webpart zone inside custom aspx?

    Question

  • I created a custom aspx that lies in the 12 /Layouts directory. Is it possible to add a WebPart zone and web part to it? I am trying to do it from VS but I keep on getting list exceptions.

    Victor Palma
    Wednesday, January 20, 2010 11:15 PM

Answers

  • Here you go:

    <%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" AutoEventWireup="true" Inherits="MyNamespace.MyClass, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenValue" %>
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
      Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="MyWebPart" Namespace="MyNamespace" Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenValue" %>
    <%@ Register tagprefix="WebPartPages" namespace="Microsoft.SharePoint.WebPartPages" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
     
    <asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <WebPartPages:WebPartZone id="WPZ_MyWebPart_1" runat="server" title="Zone 2">
            <ZoneTemplate>
                <MyWebPart:WebPartClassName runat="server" ID="WP_MyWebPart_1" Title="Mighty Web Part" ChromeType="TitleOnly" />
            </ZoneTemplate>
        </WebPartPages:WebPartZone>
    </asp:Content>

    So what I have done just <%@ Register ... %> directives in order to use SharePoint functionality.

    Hope that helps.

    Best wishes,

    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Thursday, January 21, 2010 12:02 PM
  • Here is an example that will add a WebpartZone into a TD and also add a GenericWebPart into the WebPartZone. The Zone's can't be added after the Page_Init event:

    <%@ Page Language="C#" ClassName="Default_aspx" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <script runat="server">
        public void Page_Init()
        {
            Button button = new Button();
            button.ID = "testButton";

            WebPart myWebPart = WebPartManager1.CreateWebPart(button);
            WebPartZone zone1 = new WebPartZone();
            zone1.ID = "zone1";
            MyZone.Controls.Add(zone1);
            WebPartManager1.AddWebPart(myWebPart, zone1, 0);
        }    

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:WebPartManager ID="WebPartManager1" runat="server">
            </asp:WebPartManager>
            <table>
                <tr>
                    <td id="MyZone" runat="server">
                    </td>
                </tr>
            </table>
        </div>
        <asp:WebPartPageMenu ID="WebPartPageMenu1" Runat="server">
        </asp:WebPartPageMenu>
        </form>
    </body>
    </html>

    Cogito, ergo sum.
    • Edited by Chengyi Wu Friday, January 22, 2010 1:29 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:22 AM
  • Web Part pages in a Windows SharePoint Services 3.0 site are built on top of the new Web Part infrastructure introduced with ASP.NET 2.0. To create a Web Part page in an ASP.NET 2.0 application, you must create an .aspx page that contains exactly one instance of a control named WebPartManager and one or more WebPartZone controls. The WebPartManager is responsible for managing the lifetime of Web Part instances as well as serializing Web Part–related data so that it can be stored and retrieved from the tables in the ASP.NET services database.

    The Web Part infrastructure of Windows SharePoint Services 3.0 does not use the standard WebPartManager control from ASP.NET. Instead, Windows SharePoint Services relies on a specialized control named SPWebPartManager that derives from the ASP.NET 2.0 WebPartManager control. The SPWebPartManager control overrides the standard behavior of the WebPartManager control to persist Web Part data inside the Windows SharePoint Services content database instead of inside the ASP.NET services database.

    In most cases, you don't have to worry about dealing with the SPWebPartManager control directly because the one and only required instance of the SPWebPartManager is already defined in the standard default.master page. When you create a site page that links to default.master, the SPWebPartManager control is automatically added to the page. Therefore, you simply need to add one or more WebPartZone controls.

    Two things must be done when creating a page template for a Web Part page. The first is to inherit from the WebPartPage class that is defined inside the Microsoft.SharePoint.dll assembly. The second is to add one or more WebPartZone controls. Note that you must use the WebPartZone control defined by the Windows SharePoint Services team and not the one of the same name defined by the ASP.NET team.

    To add WebPartZone controls to a page template, you must add a @ Register directive that imports all of the controls from the Microsoft.SharePoint.dll assembly defined in the Microsoft.SharePoint.WebPartPages namespace as shown in the following page template definition.

    --------------------------SAMPLE CODE------------------------------------------------------
    <%@ Page MasterPageFile="~masterurl/default.master" 
        Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,
                  Microsoft.SharePoint, [full 4-part assembly name]"
        meta:progid="SharePoint.WebPartPage.Document"   %>

    <%@ Register Tagprefix="WebPartPages" 
        Namespace="Microsoft.SharePoint.WebPartPages" 
        Assembly="Microsoft.SharePoint, ..." %>

    <asp:Content ID="main" runat="server" ContentPlaceHolderID="PlaceHolderMain" >

    <h3>Custom Web Part page</h3>

    <table width="100%">
      <tr>
        <td valign="top" style="width:50%">
            <WebPartPages:WebPartZone ID="LeftZone" runat="server" 
                                      FrameType="TitleBarOnly" 
                                      Title="Left Web Part zone" />        
        </td>
        <td valign="top" style="width:50%">
            <WebPartPages:WebPartZone ID="RightZone" runat="server" 
                                      FrameType="TitleBarOnly" 
                                      Title="Right Web Part zone" />        
        </td>
      </tr>
    </table>
    </asp:Content>

    It is easier to create pages that host Web Parts in the Windows SharePoint Services framework than in the ASP.NET Framework. For example, when you design an ASP.NET application that involves Web Parts, you are required to add logic to each page that interacts with the WebPartManager control to manage the display mode. It is also necessary to explicitly add controls, such as Editor Zones and Catalog Zones, to the page so that users can customize existing Web Parts as well as add new Web Parts.

    More reference at http://msdn.microsoft.com/en-us/library/bb964680.aspx

    Cogito, ergo sum.
    • Edited by Chengyi Wu Friday, January 22, 2010 1:30 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:29 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:29 AM

All replies

  • Hi Victor,

    Yes, it is possible. In VS open your aspx in source mode and just input the markup for your WebPart zone and zone template with WebPart in it.

    That should work.

    Please have a look at the following article - section "Designing Web Part Pages" - http://msdn.microsoft.com/en-us/library/bb964680.aspx

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Edited by Slava Gorbunov Thursday, January 21, 2010 12:01 AM additional info
    Wednesday, January 20, 2010 11:58 PM
  • Hey Slava,

    Thanks for the reply. Yes almost close but ran accross one little issue! Unfortunately, my custom aspx that lies in the layouts folder, is inheriting already from my Solution namespace. In the sample given, you must inherit from the

    Microsoft.SharePoint.WebPartPages.WebPartPage

    namespace in order to get that sample working:

    <%@ Page MasterPageFile="~masterurl/default.master"
    
      
    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,
    Microsoft.SharePoint, [full 4-part assembly name]"

    meta:progid="SharePoint.WebPartPage.Document" %>

    Victor Palma
    Thursday, January 21, 2010 12:41 AM
  • Hey Victor,

    It's not necessary. Your custom aspx page can be inherited from anything you want. I can send you the sample later, because I have done it myself.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    Thursday, January 21, 2010 1:19 AM
  • Here you go:

    <%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" AutoEventWireup="true" Inherits="MyNamespace.MyClass, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenValue" %>
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
      Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="MyWebPart" Namespace="MyNamespace" Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenValue" %>
    <%@ Register tagprefix="WebPartPages" namespace="Microsoft.SharePoint.WebPartPages" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
     
    <asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <WebPartPages:WebPartZone id="WPZ_MyWebPart_1" runat="server" title="Zone 2">
            <ZoneTemplate>
                <MyWebPart:WebPartClassName runat="server" ID="WP_MyWebPart_1" Title="Mighty Web Part" ChromeType="TitleOnly" />
            </ZoneTemplate>
        </WebPartPages:WebPartZone>
    </asp:Content>

    So what I have done just <%@ Register ... %> directives in order to use SharePoint functionality.

    Hope that helps.

    Best wishes,

    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Thursday, January 21, 2010 12:02 PM
  • Here is an example that will add a WebpartZone into a TD and also add a GenericWebPart into the WebPartZone. The Zone's can't be added after the Page_Init event:

    <%@ Page Language="C#" ClassName="Default_aspx" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <script runat="server">
        public void Page_Init()
        {
            Button button = new Button();
            button.ID = "testButton";

            WebPart myWebPart = WebPartManager1.CreateWebPart(button);
            WebPartZone zone1 = new WebPartZone();
            zone1.ID = "zone1";
            MyZone.Controls.Add(zone1);
            WebPartManager1.AddWebPart(myWebPart, zone1, 0);
        }    

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:WebPartManager ID="WebPartManager1" runat="server">
            </asp:WebPartManager>
            <table>
                <tr>
                    <td id="MyZone" runat="server">
                    </td>
                </tr>
            </table>
        </div>
        <asp:WebPartPageMenu ID="WebPartPageMenu1" Runat="server">
        </asp:WebPartPageMenu>
        </form>
    </body>
    </html>

    Cogito, ergo sum.
    • Edited by Chengyi Wu Friday, January 22, 2010 1:29 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:22 AM
  • Web Part pages in a Windows SharePoint Services 3.0 site are built on top of the new Web Part infrastructure introduced with ASP.NET 2.0. To create a Web Part page in an ASP.NET 2.0 application, you must create an .aspx page that contains exactly one instance of a control named WebPartManager and one or more WebPartZone controls. The WebPartManager is responsible for managing the lifetime of Web Part instances as well as serializing Web Part–related data so that it can be stored and retrieved from the tables in the ASP.NET services database.

    The Web Part infrastructure of Windows SharePoint Services 3.0 does not use the standard WebPartManager control from ASP.NET. Instead, Windows SharePoint Services relies on a specialized control named SPWebPartManager that derives from the ASP.NET 2.0 WebPartManager control. The SPWebPartManager control overrides the standard behavior of the WebPartManager control to persist Web Part data inside the Windows SharePoint Services content database instead of inside the ASP.NET services database.

    In most cases, you don't have to worry about dealing with the SPWebPartManager control directly because the one and only required instance of the SPWebPartManager is already defined in the standard default.master page. When you create a site page that links to default.master, the SPWebPartManager control is automatically added to the page. Therefore, you simply need to add one or more WebPartZone controls.

    Two things must be done when creating a page template for a Web Part page. The first is to inherit from the WebPartPage class that is defined inside the Microsoft.SharePoint.dll assembly. The second is to add one or more WebPartZone controls. Note that you must use the WebPartZone control defined by the Windows SharePoint Services team and not the one of the same name defined by the ASP.NET team.

    To add WebPartZone controls to a page template, you must add a @ Register directive that imports all of the controls from the Microsoft.SharePoint.dll assembly defined in the Microsoft.SharePoint.WebPartPages namespace as shown in the following page template definition.

    --------------------------SAMPLE CODE------------------------------------------------------
    <%@ Page MasterPageFile="~masterurl/default.master" 
        Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,
                  Microsoft.SharePoint, [full 4-part assembly name]"
        meta:progid="SharePoint.WebPartPage.Document"   %>

    <%@ Register Tagprefix="WebPartPages" 
        Namespace="Microsoft.SharePoint.WebPartPages" 
        Assembly="Microsoft.SharePoint, ..." %>

    <asp:Content ID="main" runat="server" ContentPlaceHolderID="PlaceHolderMain" >

    <h3>Custom Web Part page</h3>

    <table width="100%">
      <tr>
        <td valign="top" style="width:50%">
            <WebPartPages:WebPartZone ID="LeftZone" runat="server" 
                                      FrameType="TitleBarOnly" 
                                      Title="Left Web Part zone" />        
        </td>
        <td valign="top" style="width:50%">
            <WebPartPages:WebPartZone ID="RightZone" runat="server" 
                                      FrameType="TitleBarOnly" 
                                      Title="Right Web Part zone" />        
        </td>
      </tr>
    </table>
    </asp:Content>

    It is easier to create pages that host Web Parts in the Windows SharePoint Services framework than in the ASP.NET Framework. For example, when you design an ASP.NET application that involves Web Parts, you are required to add logic to each page that interacts with the WebPartManager control to manage the display mode. It is also necessary to explicitly add controls, such as Editor Zones and Catalog Zones, to the page so that users can customize existing Web Parts as well as add new Web Parts.

    More reference at http://msdn.microsoft.com/en-us/library/bb964680.aspx

    Cogito, ergo sum.
    • Edited by Chengyi Wu Friday, January 22, 2010 1:30 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:29 AM
    • Marked as answer by Chengyi Wu Friday, January 29, 2010 2:38 AM
    Friday, January 22, 2010 1:29 AM
  • Hi,

     

    I was able to successfully implement this with Web Part connections in MOSS 2007 but I was not able to implement this in SharePoint 2010.

    I referenced the correct assembly versions, but it was not helpful. I was not even able to register my custom Web Parts in it.

    Can some one help me with this.

     

    Thanks

    Carol

    Wednesday, June 30, 2010 1:26 AM