SPWeb.GetListFromWebPartPageUrl Method

Gets the list that is associated with the first Web Part on the specified Web Parts page.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function GetListFromWebPartPageUrl ( _
    pageUrl As String _
) As SPList
'Usage
Dim instance As SPWeb
Dim pageUrl As String
Dim returnValue As SPList

returnValue = instance.GetListFromWebPartPageUrl(pageUrl)
public SPList GetListFromWebPartPageUrl(
    string pageUrl
)

Parameters

  • pageUrl
    Type: System.String

    The server-relative URL of a Web Part page, such as /sites/sitecollection/subsite/default.aspx.

Return Value

Type: Microsoft.SharePoint.SPList
The list that is associated with the first Web Part on the specified page.

Exceptions

Exception Condition
ArgumentNullException

pageUrl is null .

SPException

pageUrlis not a valid URL.

-or-

The first Web Part on the specified Web Parts page is not associated with lists.

Remarks

This method returns the list that is associated with the first Web Part on the specified Web Parts page. To return the list that is associated with the Web Parts page itself, use the GetList method.

Examples

The following example is a console application that demonstrates how to get an SPList object that represents the list that is associated with the first Web Part on the default page of a website.

Note that the example assumes the existence of a site collection with an absolute URL of https://localhost/sites/sitecollection and that this site collection has a website named subsite.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://localhost/sites/sitecollection")
         Using web As SPWeb = site.OpenWeb("subsite")

            Dim pageUrl As String = "/sites/sitecollection/subsite/default.aspx"
            Dim list As SPList = web.GetListFromWebPartPageUrl(pageUrl)
            Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl)

         End Using
      End Using
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://localhost/sites/sitecollection"))
         {
            using (SPWeb web = site.OpenWeb("subsite"))
            {
               string pageUrl = "/sites/sitecollection/subsite/default.aspx";
               SPList list = web.GetListFromWebPartPageUrl(pageUrl);
               Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
            }
         }
         Console.ReadLine();
      }
   }
}

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace