Skip to content

Commit

Permalink
Bugfixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenZomers committed Aug 1, 2023
1 parent d00fb00 commit 930b9cd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
14 changes: 14 additions & 0 deletions documentation/Get-PnPFolderFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -ExcludeSystemFolders
When provided, all files in system folders will be excluded from the output. This parameter is not supported when not providing a folder through -Identity or -FolderSiteRelativeUrl.
```yaml
Type: SwitchParameter
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -FolderSiteRelativeUrl
The site relative URL of the folder to retrieve
Expand Down
2 changes: 1 addition & 1 deletion documentation/Get-PnPFolderFolder.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Accept wildcard characters: False
```
### -ExcludeSystemFolders
When provided, all system folders will be excluded from the output
When provided, all system folders will be excluded from the output. This parameter is not supported when not providing a folder through -Identity or -FolderSiteRelativeUrl.
```yaml
Type: SwitchParameter
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Files/GetFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void ExecuteCmdlet()
case ParameterSet_FOLDERSINCURRENTWEB:
{
// Query for all folders in the root of the current web
ClientContext.Load(CurrentWeb, w => w.Folders.Include(DefaultRetrievalExpressions));
ClientContext.Load(CurrentWeb, w => w.Folders.IncludeWithDefaultProperties(RetrievalExpressions));
ClientContext.ExecuteQueryRetry();

WriteObject(CurrentWeb.Folders, true);
Expand Down
29 changes: 26 additions & 3 deletions src/Commands/Files/GetFolderFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.Framework.Utilities;
Expand All @@ -26,9 +27,12 @@ public class GetFolderFile : PnPWebRetrievalsCmdlet<File>
[Parameter(Mandatory = false)]
public string ItemName = string.Empty;

[Parameter(Mandatory = false, Position = 4)]
[Parameter(Mandatory = false)]
public SwitchParameter Recursive;

[Parameter(Mandatory = false)]
public SwitchParameter ExcludeSystemFolders;

protected override void ExecuteCmdlet()
{
CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
Expand Down Expand Up @@ -58,7 +62,19 @@ private IEnumerable<File> GetContents(string FolderSiteRelativeUrl)
serverRelativeUrl = UrlUtility.Combine(CurrentWeb.ServerRelativeUrl, FolderSiteRelativeUrl);
}

targetFolder = (string.IsNullOrEmpty(FolderSiteRelativeUrl)) ? CurrentWeb.RootFolder : CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
if(string.IsNullOrEmpty(FolderSiteRelativeUrl))
{
if(ParameterSpecified(nameof(ExcludeSystemFolders)))
{
WriteWarning($"The {nameof(ExcludeSystemFolders)} parameter is only supported when retrieving a specific folder. It will be ignored.");
ExcludeSystemFolders = false;
}
targetFolder = CurrentWeb.EnsureProperty(w => w.RootFolder);
}
else
{
targetFolder = CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
}
}

IEnumerable<File> files = null;
Expand All @@ -68,7 +84,14 @@ private IEnumerable<File> GetContents(string FolderSiteRelativeUrl)

if (Recursive)
{
folders = ClientContext.LoadQuery(targetFolder.Folders).OrderBy(f => f.Name);
if(ExcludeSystemFolders.ToBool())
{
folders = ClientContext.LoadQuery(targetFolder.Folders.IncludeWithDefaultProperties(f => f.ListItemAllFields)).Where(f => !ExcludeSystemFolders.ToBool() || !f.ListItemAllFields.ServerObjectIsNull.GetValueOrDefault(false)).OrderBy(f => f.Name);
}
else
{
folders = ClientContext.LoadQuery(targetFolder.Folders).OrderBy(f => f.Name);
}
}
ClientContext.ExecuteQueryRetry();

Expand Down
31 changes: 25 additions & 6 deletions src/Commands/Files/GetFolderFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GetFolderFolder : PnPWebRetrievalsCmdlet<Folder>
[Parameter(Mandatory = false)]
public string ItemName = string.Empty;

[Parameter(Mandatory = false, Position = 4)]
[Parameter(Mandatory = false)]
public SwitchParameter Recursive;

[Parameter(Mandatory = false)]
Expand All @@ -35,7 +35,7 @@ public class GetFolderFolder : PnPWebRetrievalsCmdlet<Folder>
protected override void ExecuteCmdlet()
{
CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);

if(ExcludeSystemFolders.ToBool())
{
DefaultRetrievalExpressions = new Expression<Func<Folder, object>>[] { f => f.ListItemAllFields };
Expand Down Expand Up @@ -64,14 +64,33 @@ private IEnumerable<Folder> GetContents(string FolderSiteRelativeUrl)
string serverRelativeUrl = null;
if (!string.IsNullOrEmpty(FolderSiteRelativeUrl))
{
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
serverRelativeUrl = UrlUtility.Combine(webUrl, FolderSiteRelativeUrl);
serverRelativeUrl = UrlUtility.Combine(CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl), FolderSiteRelativeUrl);
}

targetFolder = (string.IsNullOrEmpty(FolderSiteRelativeUrl)) ? CurrentWeb.RootFolder : CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
if(string.IsNullOrEmpty(FolderSiteRelativeUrl))
{
if(ParameterSpecified(nameof(ExcludeSystemFolders)))
{
WriteWarning($"The {nameof(ExcludeSystemFolders)} parameter is only supported when retrieving a specific folder. It will be ignored.");
ExcludeSystemFolders = false;
}
targetFolder = CurrentWeb.EnsureProperty(w => w.RootFolder);
}
else
{
targetFolder = CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
}
}

IEnumerable<Folder> folders = ClientContext.LoadQuery(targetFolder.Folders.IncludeWithDefaultProperties(RetrievalExpressions)).Where(f => !ExcludeSystemFolders.ToBool() || !f.ListItemAllFields.ServerObjectIsNull.GetValueOrDefault(false)).OrderBy(f => f.Name);
IEnumerable<Folder> folders = null;
if(ExcludeSystemFolders.ToBool())
{
folders = ClientContext.LoadQuery(targetFolder.Folders.IncludeWithDefaultProperties(f => f.ListItemAllFields)).Where(f => !ExcludeSystemFolders.ToBool() || !f.ListItemAllFields.ServerObjectIsNull.GetValueOrDefault(false)).OrderBy(f => f.Name);
}
else
{
folders = ClientContext.LoadQuery(targetFolder.Folders).OrderBy(f => f.Name);
}
ClientContext.ExecuteQueryRetry();

IEnumerable<Folder> folderContent = folders;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Files/GetFolderItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GetFolderItem : PnPWebCmdlet
[Parameter(Mandatory = false)]
public string ItemName = string.Empty;

[Parameter(Mandatory = false, Position = 4)]
[Parameter(Mandatory = false)]
public SwitchParameter Recursive;

protected override void ExecuteCmdlet()
Expand Down

0 comments on commit 930b9cd

Please sign in to comment.