diff --git a/documentation/Get-PnPFolder.md b/documentation/Get-PnPFolder.md index 6f62e8e4d..51f6a2764 100644 --- a/documentation/Get-PnPFolder.md +++ b/documentation/Get-PnPFolder.md @@ -14,11 +14,16 @@ Returns a folder object ## SYNTAX -### Root folder of the current Web (Default) +### Folders in current Web (Default) ```powershell Get-PnPFolder [-Includes ] [-Connection ] [-Verbose] ``` +### Root folder of the current Web +```powershell +Get-PnPFolder -CurrentWebRootFolder [-Includes ] [-Connection ] [-Verbose] +``` + ### Folder by url ```powershell Get-PnPFolder -Url [-Includes ] [-Connection ] [-Verbose] @@ -49,30 +54,37 @@ Use [Get-PnPFolderItem](Get-PnPFolderItem.md) to retrieve files and subfolders. Get-PnPFolder ``` -Returns the folders instance of the root of the current web +Returns all the folders located in the root of the current web ### EXAMPLE 2 ```powershell +Get-PnPFolder -CurrentWebRootFolder +``` + +Returns the folder instance representing the root of the current web + +### EXAMPLE 3 +```powershell Get-PnPFolder -Url "Shared Documents" ``` Returns the folder called 'Shared Documents' which is located in the root of the current web -### EXAMPLE 3 +### EXAMPLE 4 ```powershell Get-PnPFolder -Url "/sites/demo/Shared Documents" ``` Returns the folder called 'Shared Documents' which is located in the root of the site collection located at '/sites/demo' -### EXAMPLE 4 +### EXAMPLE 5 ```powershell Get-PnPFolder -ListRootFolder "Shared Documents" ``` Returns the root folder of the list called 'Shared Documents' -### EXAMPLE 5 +### EXAMPLE 6 ```powershell Get-PnPFolder -List "Shared Documents" ``` @@ -81,6 +93,20 @@ Returns the folders inside the root folder of the list called 'Shared Documents' ## PARAMETERS +### -CurrentWebRootFolder +If provided, the folder representing the root of the current web will be returned + +```yaml +Type: SwitchParameter +Parameter Sets: Root folder of the current Web + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Connection Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. diff --git a/src/Commands/Files/GetFolder.cs b/src/Commands/Files/GetFolder.cs index 28b684304..7c14c9e9c 100644 --- a/src/Commands/Files/GetFolder.cs +++ b/src/Commands/Files/GetFolder.cs @@ -9,9 +9,10 @@ namespace PnP.PowerShell.Commands.Files { - [Cmdlet(VerbsCommon.Get, "PnPFolder", DefaultParameterSetName = ParameterSet_CURRENTWEBROOTFOLDER)] + [Cmdlet(VerbsCommon.Get, "PnPFolder", DefaultParameterSetName = ParameterSet_FOLDERSINCURRENTWEB)] public class GetFolder : PnPWebRetrievalsCmdlet { + private const string ParameterSet_FOLDERSINCURRENTWEB = "Folders in current Web"; private const string ParameterSet_CURRENTWEBROOTFOLDER = "Root folder of the current Web"; private const string ParameterSet_LISTROOTFOLDER = "Root folder of a list"; private const string ParameterSet_FOLDERSINLIST = "Folders In List"; @@ -28,6 +29,9 @@ public class GetFolder : PnPWebRetrievalsCmdlet [Parameter(Mandatory = true, Position = 1, ParameterSetName = ParameterSet_LISTROOTFOLDER)] public ListPipeBind ListRootFolder; + [Parameter(Mandatory = true, Position = 1, ParameterSetName = ParameterSet_CURRENTWEBROOTFOLDER)] + public SwitchParameter CurrentWebRootFolder; + protected override void ExecuteCmdlet() { DefaultRetrievalExpressions = new Expression>[] { f => f.ServerRelativeUrl, f => f.Name, f => f.TimeLastModified, f => f.ItemCount }; @@ -35,10 +39,21 @@ protected override void ExecuteCmdlet() Folder folder = null; switch(ParameterSetName) { + case ParameterSet_FOLDERSINCURRENTWEB: + { + WriteVerbose("Getting all folders in the root of the current web"); + ClientContext.Load(CurrentWeb, w => w.Folders.IncludeWithDefaultProperties(RetrievalExpressions)); + ClientContext.ExecuteQueryRetry(); + WriteObject(CurrentWeb.Folders, true); + break; + } + case ParameterSet_CURRENTWEBROOTFOLDER: { WriteVerbose("Getting root folder of the current web"); folder = CurrentWeb.RootFolder; + + ReturnFolderProperties(folder); break; } @@ -47,6 +62,8 @@ protected override void ExecuteCmdlet() WriteVerbose("Getting root folder of the provided list"); var list = ListRootFolder.GetList(CurrentWeb); folder = list.RootFolder; + + ReturnFolderProperties(folder); break; } @@ -91,13 +108,18 @@ protected override void ExecuteCmdlet() Url = UrlUtility.Combine(webServerRelativeUrl, Url); } folder = CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(Url)); + + ReturnFolderProperties(folder); break; } } + } + private void ReturnFolderProperties(Folder folder) + { WriteVerbose("Retrieving folder properties"); folder?.EnsureProperties(RetrievalExpressions); - WriteObject(folder); + WriteObject(folder, false); } } }