Skip to content

Commit c971d0e

Browse files
authored
Merge pull request #4805 from KoenZomers/GetPnPPageAllPages
Changed `Get-PnPPage` to also allow all site pages to be returned
2 parents ee3658c + 578a92b commit c971d0e

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6262
- Added `-Name` parameter to `Add-PnPApplicationCustomizer` cmdlet to allow for specifying the name of the application customizer [#4767](https://github.com/pnp/powershell/pull/4767)
6363
- Added `Get-PnPTraceLog` cmdlet which allows reading from the detailed in memory logs of the PnP PowerShell cmdlet execution [#4794](https://github.com/pnp/powershell/pull/4794)
6464
- Added `-Transitive` parameter to `Get-PnPAzureADGroupMember` cmdlet to allow members of groups inside groups to be retrieved [#4799](https://github.com/pnp/powershell/pull/4799)
65+
- Added the ability to `Get-PnPPage` to return all site pages in the site by omitting the `-Identity` parameter [#4805](https://github.com/pnp/powershell/pull/4805)
6566

6667
### Changed
6768

documentation/Get-PnPPage.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ online version: https://pnp.github.io/powershell/cmdlets/Get-PnPPage.html
1010
# Get-PnPPage
1111

1212
## SYNOPSIS
13-
Returns a modern page
13+
Returns a specific or all modern pages (Site Pages) in a SharePoint site.
1414

1515
## SYNTAX
1616

17+
### Specific page
18+
1719
```powershell
1820
Get-PnPPage -Identity <PagePipeBind> [-Connection <PnPConnection>]
1921
```
2022

23+
### All pages
24+
25+
```powershell
26+
Get-PnPPage [-Connection <PnPConnection>]
27+
```
28+
2129
## DESCRIPTION
2230
This command allows the retrieval of a modern sitepage along with its properties and contents on it. Note that for a newly created modern site, the Columns and Sections of the Home.aspx page will not be filled according to the actual site page contents. This is because the underlying CanvasContent1 will not be populated until the homepage has been edited and published. The reason for this behavior is to allow for the default homepage to be able to be updated by Microsoft as long as it hasn't been modified. For any other site page or after editing and publishing the homepage, this command will return the correct columns and sections as they are positioned on the site page.
2331

32+
Also note that if you want to retrieve all site pages in a site by omitting the -Identity parameter, the command will return all pages in the Site Pages library, but with less details and will require SharePoint Online Administrator permissions to do so. This is how it has been designed on the server side. If you want the full details on all pages, you can do it as follows `Get-PnPPage | % { Get-PnPPage -Identity $_.Name }`. Be aware that this causes a lot of server calls and is much slower than the first command.
33+
2434
## EXAMPLES
2535

2636
### EXAMPLE 1
@@ -51,6 +61,13 @@ Get-PnPPage -Identity "MyPage.aspx" -Web (Get-PnPWeb -Identity "Subsite1")
5161

5262
Gets the page named 'MyPage.aspx' from the subsite named 'Subsite1'
5363

64+
### EXAMPLE 5
65+
```powershell
66+
Get-PnPPage
67+
```
68+
69+
Returns all site pages in the current SharePoint site. Note that this will return less details than when using the -Identity parameter and requires SharePoint Online Administrator permissions to do so.
70+
5471
## PARAMETERS
5572

5673
### -Connection
@@ -68,11 +85,11 @@ Accept wildcard characters: False
6885
```
6986
7087
### -Identity
71-
The name of the page
88+
The name of the page to retrieve
7289
7390
```yaml
7491
Type: PagePipeBind
75-
Parameter Sets: (All)
92+
Parameter Sets: Specific page
7693

7794
Required: True
7895
Position: 0
@@ -81,9 +98,6 @@ Accept pipeline input: True (ByValue)
8198
Accept wildcard characters: False
8299
```
83100
84-
85-
86101
## RELATED LINKS
87102
88-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
89-
103+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Pages/GetPage.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
1-
using PnP.PowerShell.Commands.Base.Completers;
1+
using Microsoft.Online.SharePoint.TenantAdministration;
2+
using Microsoft.SharePoint.Client;
3+
using PnP.PowerShell.Commands.Base.Completers;
24
using PnP.PowerShell.Commands.Base.PipeBinds;
5+
using PnP.PowerShell.Commands.Utilities;
36
using System;
7+
using System.Collections.Generic;
48
using System.Management.Automation;
9+
using System.Reflection.Metadata;
510

611
namespace PnP.PowerShell.Commands.Pages
712
{
8-
[Cmdlet(VerbsCommon.Get, "PnPPage")]
13+
[Cmdlet(VerbsCommon.Get, "PnPPage", DefaultParameterSetName = ParameterSet_ALL)]
914
[Alias("Get-PnPClientSidePage")]
10-
[OutputType(typeof(PnP.Core.Model.SharePoint.IPage))]
15+
[OutputType(typeof(Core.Model.SharePoint.IPage), ParameterSetName = new[] { ParameterSet_SPECIFICPAGE })]
16+
[OutputType(typeof(IEnumerable<SPSitePage>), ParameterSetName = new[] { ParameterSet_ALL })]
1117
public class GetPage : PnPWebCmdlet
1218
{
13-
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
19+
private const string ParameterSet_SPECIFICPAGE = "Specific page";
20+
private const string ParameterSet_ALL = "All pages";
21+
22+
[Parameter(Mandatory = false, ValueFromPipeline = true, Position = 0, ParameterSetName = ParameterSet_SPECIFICPAGE)]
1423
[ArgumentCompleter(typeof(PageCompleter))]
1524
public PagePipeBind Identity;
1625

1726
protected override void ExecuteCmdlet()
1827
{
19-
var clientSidePage = Identity.GetPage(Connection);
28+
if (!ParameterSpecified(nameof(Identity)))
29+
{
30+
var tenantUrl = Connection.TenantAdminUrl ?? UrlUtilities.GetTenantAdministrationUrl(ClientContext.Url);
31+
32+
using var tenantContext = ClientContext.Clone(tenantUrl);
33+
var tenant = new Tenant(tenantContext);
34+
35+
CurrentWeb.EnsureProperty(w => w.Url);
36+
var pages = tenant.GetSPSitePages(CurrentWeb.Url);
37+
tenantContext.ExecuteQueryRetry();
38+
39+
WriteObject(pages, true);
40+
}
41+
else
42+
{
43+
var clientSidePage = Identity.GetPage(Connection);
2044

21-
if (clientSidePage == null)
22-
throw new Exception($"Page '{Identity?.Name}' does not exist");
45+
if (clientSidePage == null)
46+
throw new Exception($"Page '{Identity?.Name}' does not exist");
2347

24-
WriteObject(clientSidePage);
48+
WriteObject(clientSidePage);
49+
}
2550
}
2651
}
2752
}

0 commit comments

Comments
 (0)