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

Several fixes in Get-PnPSubWebs and addition of -RootWeb parameter #3011

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

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

### Contributors
- David Blaszyk [https://github.com/acornsoft]
- David Blaszyk [acornsoft]
- Koen Zomers [koenzomers]

## [3.27.2011.0]

Expand Down
64 changes: 54 additions & 10 deletions Commands/Web/GetSubwebs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace PnP.PowerShell.Commands
[CmdletHelp("Returns the subwebs of the current web",
Category = CmdletHelpCategory.Webs,
OutputType = typeof(Web),
OutputTypeLink = "https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.aspx")]
OutputTypeLink = "https://docs.microsoft.com/previous-versions/office/sharepoint-server/ee537040(v=office.15)")]
[CmdletExample(
Code = @"PS:> Get-PnPSubWebs",
Remarks = "Retrieves all subsites of the current context returning the Id, Url, Title and ServerRelativeUrl of each subsite in the output",
Expand All @@ -28,8 +28,12 @@ namespace PnP.PowerShell.Commands
SortOrder = 3)]
[CmdletExample(
Code = @"PS:> Get-PnPSubWebs -Identity Team1 -Recurse",
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",
Remarks = "Retrieves all subsites of the subsite Team1 and all of its nested child subsites",
SortOrder = 4)]
[CmdletExample(
Code = @"PS:> Get-PnPSubWebs -Identity Team1 -Recurse -IncludeRootWeb",
Remarks = "Retrieves the rootweb, all subsites of the subsite Team1 and all of its nested child subsites",
SortOrder = 5)]
public class GetSubWebs : PnPWebRetrievalsCmdlet<Web>
{
[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")]
Expand All @@ -38,29 +42,69 @@ public class GetSubWebs : PnPWebRetrievalsCmdlet<Web>
[Parameter(Mandatory = false, HelpMessage = "If provided, recursion through all subsites and their children will take place to return them as well")]
public SwitchParameter Recurse;

[Parameter(Mandatory = false, HelpMessage = "If provided, the results will also contain the rootweb")]
public SwitchParameter IncludeRootWeb;

protected override void ExecuteCmdlet()
{
DefaultRetrievalExpressions = new Expression<Func<Web, object>>[] { w => w.Id, w => w.Url, w => w.Title, w => w.ServerRelativeUrl };

Web parentWeb = SelectedWeb;
List<Web> results = new List<Web>();
if(IncludeRootWeb)
{
parentWeb.EnsureProperties(RetrievalExpressions);
results.Add(parentWeb);
}

if (Identity != null)
{
if (Identity.Id != Guid.Empty)
try
{
parentWeb = parentWeb.GetWebById(Identity.Id);
if (Identity.Id != Guid.Empty)
{
parentWeb = parentWeb.GetWebById(Identity.Id);
}
else if (Identity.Web != null)
{
parentWeb = Identity.Web;
}
else if (Identity.Url != null)
{
parentWeb = parentWeb.GetWebByUrl(Identity.Url);
}
}
else if (Identity.Web != null)
catch(ServerException e) when (e.ServerErrorTypeName.Equals("System.IO.FileNotFoundException"))
{
parentWeb = Identity.Web;
throw new PSArgumentException($"No subweb found with the provided id or url", nameof(Identity));
}
else if (Identity.Url != null)

if (parentWeb != null)
{
parentWeb = parentWeb.GetWebByUrl(Identity.Url);
if (Recurse)
{
results.Add(parentWeb);
results.AddRange(GetSubWebsInternal(parentWeb.Webs, Recurse));
}
else
{
results.Add(parentWeb);
}
}
else
{
throw new PSArgumentException($"No subweb found with the provided id or url", nameof(Identity));
}
}
else
{
ClientContext.Load(parentWeb.Webs);
ClientContext.ExecuteQueryRetry();

results.AddRange(GetSubWebsInternal(parentWeb.Webs, Recurse));
}

var allWebs = GetSubWebsInternal(parentWeb.Webs, Recurse);
WriteObject(allWebs, true);
WriteObject(results, true);
}

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