Skip to content

Commit aef6524

Browse files
committed
Add -PageSize parameter.
1 parent 26a9284 commit aef6524

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

src/readme.graph.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ directive:
8080
- where:
8181
parameter-name: Top
8282
set:
83-
parameter-name: PageSize
8483
alias:
85-
- Top
8684
- Limit
8785
- where:
8886
parameter-name: Select
@@ -395,7 +393,7 @@ directive:
395393
return $;
396394
}
397395
398-
# Add custom -All parameter to *_List cmdlets that support Odata next link.
396+
# Add custom -PageSize parameter to *_List cmdlets that support Odata next link.
399397
- from: source-file-csharp
400398
where: $
401399
transform: >
@@ -411,7 +409,7 @@ directive:
411409
$ = $.replace(psBaseClassImplementationRegex, '$1Microsoft.Graph.PowerShell.Cmdlets.Custom.ListCmdlet');
412410
413411
let beginProcessingRegex = /(^\s*)(protected\s*override\s*void\s*BeginProcessing\(\)\s*{)/gmi
414-
$ = $.replace(beginProcessingRegex, '$1$2\n$1$1if (this.InvocationInformation.BoundParameters.ContainsKey("PageSize")){ InitializePaging(ref this.__invocationInfo, ref this._pageSize); }\n$1');
412+
$ = $.replace(beginProcessingRegex, '$1$2\n$1 if (this.InvocationInformation?.BoundParameters != null){ InitializePaging(ref this.__invocationInfo, ref this._top); }\n$1');
415413
416414
let odataNextLinkCallRegex = /(^\s*)(await\s*this\.Client\.UsersUserListUser_Call\(requestMessage\,\s*onOk\,\s*onDefault\,\s*this\,\s*Pipeline\)\;)/gmi
417415
$ = $.replace(odataNextLinkCallRegex, '$1requestMessage.RequestUri = GetOverflowItemsNextLinkUri(requestMessage.RequestUri);\n$1$2');

tools/Custom/ListCmdlet.cs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
// ------------------------------------------------------------------------------
44
namespace Microsoft.Graph.PowerShell.Cmdlets.Custom
55
{
6+
using System;
7+
using System.Management.Automation;
68

79
public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
810
{
9-
/// <summary>Backing field for <see cref="All" /> property.</summary>
10-
private global::System.Management.Automation.SwitchParameter _all;
11-
12-
/// <summary>List All pages</summary>
13-
[global::System.Management.Automation.Parameter(Mandatory = false, HelpMessage = "List all pages")]
11+
/// <summary>Backing field for <see cref="PageSize" /> property.</summary>
12+
private int _pageSize;
13+
14+
/// <summary>Sets the page size of results.</summary>
15+
[global::System.Management.Automation.Parameter(Mandatory = false, HelpMessage = "Sets the page size of results.")]
1416
[Microsoft.Graph.PowerShell.Runtime.Info(
1517
Required = false,
1618
ReadOnly = false,
17-
Description = @"List all pages",
18-
SerializedName = @"$all",
19+
Description = @"The page size of results.",
1920
PossibleTypes = new[] { typeof(global::System.Management.Automation.SwitchParameter) })]
2021
[global::Microsoft.Graph.PowerShell.Category(global::Microsoft.Graph.PowerShell.ParameterCategory.Runtime)]
21-
public global::System.Management.Automation.SwitchParameter All { get => this._all; set => this._all = value; }
22+
public int PageSize { get => this._pageSize; set => this._pageSize = value; }
2223

2324
/// <summary>
2425
/// Default number of items per page.
@@ -30,11 +31,6 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
3031
/// </summary>
3132
internal const int MaxPageSize = 999;
3233

33-
/// <summary>
34-
/// Original page size/top/limit passed to Cmdlet via parameter.
35-
/// </summary>
36-
internal int originalPageSize = 0;
37-
3834
/// <summary>
3935
/// Total number of pages required to iterate page collections excluding overflow items.
4036
/// </summary>
@@ -56,26 +52,47 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
5652
/// </summary>
5753
internal int totalFetchedItems = 0;
5854

55+
/// <summary>
56+
/// Total number of items to be fetched.
57+
/// </summary>
58+
internal int limit = default;
59+
5960
/// <summary>
6061
/// Initializes paging values.
6162
/// </summary>
6263
/// <param name="invocationInfo">A reference to <see cref="System.Management.Automation.InvocationInfo"/> object.</param>
63-
/// <param name="PageSize">A reference to page size parameter.</param>
64-
public void InitializePaging(ref global::System.Management.Automation.InvocationInfo invocationInfo, ref int PageSize)
64+
/// <param name="top">A reference to top parameter.</param>
65+
public void InitializePaging(ref global::System.Management.Automation.InvocationInfo invocationInfo, ref int top)
6566
{
66-
if (PageSize > MaxPageSize)
67+
if (invocationInfo.BoundParameters.ContainsKey("PageSize") && (PageSize > MaxPageSize || PageSize == default))
68+
{
69+
ThrowTerminatingError(
70+
new ErrorRecord(
71+
new ArgumentException($"Invalid page size specified `{PageSize}`. {nameof(PageSize)} must be between 1 and {MaxPageSize} inclusive."),
72+
Guid.NewGuid().ToString(),
73+
ErrorCategory.InvalidArgument,
74+
null));
75+
}
76+
77+
// Move `-Top` parameter to `limit` parameter.
78+
if (invocationInfo.BoundParameters.ContainsKey("Top"))
6779
{
68-
invocationInfo.BoundParameters["All"] = true;
69-
originalPageSize = PageSize;
70-
requiredPages = PageSize / DefaultPageSize;
71-
overflowItemsCount = PageSize % DefaultPageSize;
72-
invocationInfo.BoundParameters["PageSize"] = DefaultPageSize;
73-
PageSize = DefaultPageSize;
80+
limit = top;
81+
}
82+
83+
// Explicitly set `-PageSize` as our $top parameter.
84+
invocationInfo.BoundParameters["Top"] = invocationInfo.BoundParameters.ContainsKey("PageSize") ? PageSize : DefaultPageSize;
85+
top = (int)invocationInfo.BoundParameters["Top"];
86+
87+
if (limit != default)
88+
{
89+
requiredPages = limit / top;
90+
overflowItemsCount = limit % top;
7491
}
7592
}
7693

7794
/// <summary>
78-
/// Determines whether the cmdlet should follow the OData next link url.
95+
/// Determines whether the cmdlet should follow the OData next link URI.
7996
/// </summary>
8097
/// <param name="boundParameters">The bound parameters of the cmdlet.</param>
8198
/// <param name="itemsCount">Current page items count.</param>
@@ -84,15 +101,8 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary<str
84101
{
85102
iteratedPages++;
86103
totalFetchedItems += itemsCount;
87-
if (boundParameters.ContainsKey("All") ||
88-
(boundParameters.ContainsKey("PageSize") && totalFetchedItems < originalPageSize))
89-
{
90-
return true;
91-
}
92-
else
93-
{
94-
return false;
95-
}
104+
105+
return limit == default || totalFetchedItems < limit;
96106
}
97107

98108
/// <summary>

0 commit comments

Comments
 (0)