Partilhar via


How To: Host SharePoint features such as dynamic Web Parts and Web Part Zones in ASPX page

Error

We get an error "ListViewWebPart could not be added, list may be hidden." while putting a ListViewWebpart inside a custom ASPX page in _layouts folder.

 

Resolution / Workaround

We found the information in MSDN “Application pages, unlike content pages, cannot host SharePoint features such as dynamic Web Parts and Web Part Zones."
<https://msdn2.microsoft.com/en-us/library/aa979604.aspx>

However, we can achieve our functionality very well with the SharePoint content page as per the following steps.
1.    Make the necessary changes (i.e. List GUID etc.) in the following code and build it as a class library.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using System.Xml;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace ListViewWP
{
    [Serializable]
    public class ListViewWPPage : Page 
    {
        protected SPWebPartManager m;
        //protected ListViewWebPart wp;
        protected void Page_Init(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists[new Guid("DA5B7A41-D396-40A7-8BEA-9C3080319894")];
            list.Hidden = false;
            ListViewWebPart wp = new ListViewWebPart();
            wp.ZoneID = "Main";
            wp.ListName = list.ID.ToString("B").ToUpper();
            wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
            m.AddWebPart(wp, m.Zones["Main"], 1);

        }      
    }
}

2.    Build the class library project and deploy the dll in the bin directory of the SharePoint site’s virtual directory. (we can strong name the dll and deploy in GAC too)
3.    Add an entry within safe controls in the web.config file of the SharePoint site.
4.    Open the site in SharePoint designer.
5.    Create a new aspx page and replace all the code with the following.

<%@ Page Language="C#" Inherits="ListViewWP.ListViewWPPage" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<html dir="ltr">

<head id="Head1" runat="server">
<META name="WebPartPageExpansion" content="full">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 2</title>
</head>

<body>

<form id="form1" runat="server">

    <WebPartPages:SPWebPartManager id="m" runat="Server"/>   
    <WebPartPages:WebPartZone ID="Main" runat="server"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>

</form>

</body>

</html>

6.    Save the page and you can browse to the page now.

You have complete SharePoint object model available in the class library project so you can build all type of customizations in it.

Keywords: Dynamic Web Parts and Web Part Zones in ASPX page, ListViewWebPart could not be added, list may be hidden, ListViewWebPart, SharePoint Designer, Office SharePoint server 2007, Windows SharePoint services 3.0