Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit c31b9ba

Browse files
Merge pull request #3011 from KoenZomers/FixingGetPnPSubwebs
Several fixes in Get-PnPSubWebs and addition of -RootWeb parameter
2 parents 167880e + c01f92a commit c31b9ba

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1111

1212
### Changed
1313
- Small fixes to README.md
14+
- Fixed several issues with `Get-PnPSubwebs` and introduced optional parameter `-IncludeRootWeb` to include the rootweb in the results
1415

1516
### Contributors
16-
- David Blaszyk [https://github.com/acornsoft]
17+
- David Blaszyk [acornsoft]
18+
- Koen Zomers [koenzomers]
1719

1820
## [3.27.2011.0]
1921

Commands/Web/GetSubwebs.cs

+54-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PnP.PowerShell.Commands
1313
[CmdletHelp("Returns the subwebs of the current web",
1414
Category = CmdletHelpCategory.Webs,
1515
OutputType = typeof(Web),
16-
OutputTypeLink = "https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.aspx")]
16+
OutputTypeLink = "https://docs.microsoft.com/previous-versions/office/sharepoint-server/ee537040(v=office.15)")]
1717
[CmdletExample(
1818
Code = @"PS:> Get-PnPSubWebs",
1919
Remarks = "Retrieves all subsites of the current context returning the Id, Url, Title and ServerRelativeUrl of each subsite in the output",
@@ -28,8 +28,12 @@ namespace PnP.PowerShell.Commands
2828
SortOrder = 3)]
2929
[CmdletExample(
3030
Code = @"PS:> Get-PnPSubWebs -Identity Team1 -Recurse",
31-
Remarks = "Retrieves all subsites of the subsite Team1 and all of its nested child subsites returning the Id, Url, Title and ServerRelativeUrl of each subsite in the output",
31+
Remarks = "Retrieves all subsites of the subsite Team1 and all of its nested child subsites",
3232
SortOrder = 4)]
33+
[CmdletExample(
34+
Code = @"PS:> Get-PnPSubWebs -Identity Team1 -Recurse -IncludeRootWeb",
35+
Remarks = "Retrieves the rootweb, all subsites of the subsite Team1 and all of its nested child subsites",
36+
SortOrder = 5)]
3337
public class GetSubWebs : PnPWebRetrievalsCmdlet<Web>
3438
{
3539
[Parameter(Mandatory = false, ValueFromPipeline = true, Position = 0, HelpMessage = "If provided, only the subsite with the provided Id, GUID or the Web instance will be returned")]
@@ -38,29 +42,69 @@ public class GetSubWebs : PnPWebRetrievalsCmdlet<Web>
3842
[Parameter(Mandatory = false, HelpMessage = "If provided, recursion through all subsites and their children will take place to return them as well")]
3943
public SwitchParameter Recurse;
4044

45+
[Parameter(Mandatory = false, HelpMessage = "If provided, the results will also contain the rootweb")]
46+
public SwitchParameter IncludeRootWeb;
47+
4148
protected override void ExecuteCmdlet()
4249
{
4350
DefaultRetrievalExpressions = new Expression<Func<Web, object>>[] { w => w.Id, w => w.Url, w => w.Title, w => w.ServerRelativeUrl };
4451

4552
Web parentWeb = SelectedWeb;
53+
List<Web> results = new List<Web>();
54+
if(IncludeRootWeb)
55+
{
56+
parentWeb.EnsureProperties(RetrievalExpressions);
57+
results.Add(parentWeb);
58+
}
59+
4660
if (Identity != null)
4761
{
48-
if (Identity.Id != Guid.Empty)
62+
try
4963
{
50-
parentWeb = parentWeb.GetWebById(Identity.Id);
64+
if (Identity.Id != Guid.Empty)
65+
{
66+
parentWeb = parentWeb.GetWebById(Identity.Id);
67+
}
68+
else if (Identity.Web != null)
69+
{
70+
parentWeb = Identity.Web;
71+
}
72+
else if (Identity.Url != null)
73+
{
74+
parentWeb = parentWeb.GetWebByUrl(Identity.Url);
75+
}
5176
}
52-
else if (Identity.Web != null)
77+
catch(ServerException e) when (e.ServerErrorTypeName.Equals("System.IO.FileNotFoundException"))
5378
{
54-
parentWeb = Identity.Web;
79+
throw new PSArgumentException($"No subweb found with the provided id or url", nameof(Identity));
5580
}
56-
else if (Identity.Url != null)
81+
82+
if (parentWeb != null)
5783
{
58-
parentWeb = parentWeb.GetWebByUrl(Identity.Url);
84+
if (Recurse)
85+
{
86+
results.Add(parentWeb);
87+
results.AddRange(GetSubWebsInternal(parentWeb.Webs, Recurse));
88+
}
89+
else
90+
{
91+
results.Add(parentWeb);
92+
}
5993
}
94+
else
95+
{
96+
throw new PSArgumentException($"No subweb found with the provided id or url", nameof(Identity));
97+
}
98+
}
99+
else
100+
{
101+
ClientContext.Load(parentWeb.Webs);
102+
ClientContext.ExecuteQueryRetry();
103+
104+
results.AddRange(GetSubWebsInternal(parentWeb.Webs, Recurse));
60105
}
61106

62-
var allWebs = GetSubWebsInternal(parentWeb.Webs, Recurse);
63-
WriteObject(allWebs, true);
107+
WriteObject(results, true);
64108
}
65109

66110
private List<Web> GetSubWebsInternal(WebCollection subsites, bool recurse)

0 commit comments

Comments
 (0)