Skip to content

Commit

Permalink
Changing code to be backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenZomers committed Nov 7, 2023
1 parent 18fc085 commit 4fee800
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
36 changes: 31 additions & 5 deletions documentation/Get-PnPFolder.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <String[]>] [-Connection <PnPConnection>] [-Verbose]
```

### Root folder of the current Web
```powershell
Get-PnPFolder -CurrentWebRootFolder [-Includes <String[]>] [-Connection <PnPConnection>] [-Verbose]
```

### Folder by url
```powershell
Get-PnPFolder -Url <String> [-Includes <String[]>] [-Connection <PnPConnection>] [-Verbose]
Expand Down Expand Up @@ -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"
```
Expand All @@ -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.
Expand Down
26 changes: 24 additions & 2 deletions src/Commands/Files/GetFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Folder>
{
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";
Expand All @@ -28,17 +29,31 @@ public class GetFolder : PnPWebRetrievalsCmdlet<Folder>
[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<Func<Folder, object>>[] { f => f.ServerRelativeUrl, f => f.Name, f => f.TimeLastModified, f => f.ItemCount };

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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 4fee800

Please sign in to comment.