Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add SortByStorage parameter to GetContainer cmdlet & fix pagination #3990

Merged
merged 1 commit into from
May 31, 2024
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
19 changes: 18 additions & 1 deletion documentation/Get-PnPContainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Returns one or more Containers in a SharePoint repository services application.
## SYNTAX

```powershell
Get-PnPContainer [-Identity <ContainerPipeBind>] [-OwningApplicationId <Guid>] [-Paged <switchparameter>] [-PagingToken <string>][-Connection <PnPConnection>]
Get-PnPContainer [-Identity <ContainerPipeBind>] [-OwningApplicationId <Guid>] [-Paged <switchparameter>] [-PagingToken <string>][-SortOrder <SortOrder>] [-Connection <PnPConnection>]
```

## DESCRIPTION
Expand Down Expand Up @@ -116,6 +116,7 @@ Accept wildcard characters: False
### -PagingToken

Use this parameter to provide the <Paging Token> provided to view the remaining Containers as shown in Example 5. If there are no more Containers to display, the commandlet output will return the message End of Containers view. Otherwise, use the given <Paging Token> to retrieve the next batch of up to 5,000 ontainers.

```yaml
Type: String
Parameter Sets: (All)
Expand All @@ -126,6 +127,22 @@ Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### -SortOrder

Use this parameter to specify the sort order. The sorting will be done based on Storage used in ascending or descending order.

```yaml
Type: SortOrder
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
37 changes: 35 additions & 2 deletions src/Commands/Admin/GetContainer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.Online.SharePoint.TenantManagement;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.SharePoint;
using System;
using System.Collections.Generic;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Admin
Expand All @@ -23,6 +26,9 @@ public class GetContainer : PnPAdminCmdlet
[Parameter(Mandatory = false)]
public string PagingToken { get; set; }

[Parameter(Mandatory = false)]
public SortOrder? SortByStorage { get; set; }

protected override void ExecuteCmdlet()
{
if (Identity != null)
Expand All @@ -32,9 +38,36 @@ protected override void ExecuteCmdlet()
}
else if (OwningApplicationId != Guid.Empty)
{
var containersProperties = Tenant.GetSPOContainersByApplicationId(OwningApplicationId, Paged, PagingToken);
ClientResult<SPContainerCollection> clientResult;
if (SortByStorage.HasValue)
{
bool ascending = SortByStorage == SortOrder.Ascending;
clientResult = Tenant.GetSortedSPOContainersByApplicationId(OwningApplicationId, ascending, Paged, PagingToken);
}
else
{
clientResult = Tenant.GetSPOContainersByApplicationId(OwningApplicationId, Paged, PagingToken);
}
AdminContext.ExecuteQueryRetry();
WriteObject(containersProperties.Value.ContainerCollection);
IList<SPContainerProperties> containerCollection = clientResult.Value.ContainerCollection;
if (containerCollection != null && containerCollection.Count > 0)
{
foreach (SPContainerProperties item in containerCollection)
{
WriteObject(new Model.SharePoint.SPConsumingTenantContainerByIdentity(item));
}
if (Paged)
{
if (!string.IsNullOrWhiteSpace(clientResult.Value.PagingToken))
{
WriteObject($"Retrieve remaining containers with token: {clientResult.Value.PagingToken}");
}
else
{
WriteObject("End of containers view.");
}
}
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Microsoft.Graph;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.Online.SharePoint.TenantManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace PnP.PowerShell.Commands.Model.SharePoint
{
public class SPConsumingTenantContainerByIdentity
{

public string ContainerId { get; private set; }

public string ContainerName { get; private set; }

public string Description { get; private set; }

public Guid OwningApplicationId { get; private set; }

public string OwningApplicationName { get; private set; }

public string ContainerApiUrl { get; private set; }

public string ContainerSiteUrl { get; private set; }

public string SensitivityLabel { get; private set; }

public IList<string> Owners { get; private set; }

public IList<string> Managers { get; private set; }

public IList<string> Readers { get; private set; }

public IList<string> Writers { get; private set; }

public DateTime CreatedOn { get; private set; }

public long StorageUsedInBytes { get; private set; }

public SPOConditionalAccessPolicyType ConditionalAccessPolicy { get; private set; }

public bool AllowEditing { get; private set; }

public SPOLimitedAccessFileType LimitedAccessFileType { get; private set; }

public bool ReadOnlyForUnmanagedDevices { get; private set; }

public string AuthenticationContextName { get; private set; }

public bool BlockDownloadPolicy { get; private set; }

public bool ReadOnlyForBlockDownloadPolicy { get; private set; }

public SharingDomainRestrictionModes SharingDomainRestrictionMode { get; private set; }
public string SharingAllowedDomainList { get; private set; }
public string SharingBlockedDomainList { get; private set; }
public string Status { get; private set; }
public int OwnersCount { get; private set; }
public long StorageUsed { get; private set; }

internal SPConsumingTenantContainerByIdentity(SPContainerProperties spContainerProperties)
{
ContainerId = spContainerProperties.ContainerId;
ContainerName = spContainerProperties.ContainerName;
CreatedOn = spContainerProperties.CreatedOn;
Status = spContainerProperties.Status;
SensitivityLabel = spContainerProperties.SensitivityLabel;
OwnersCount = spContainerProperties.OwnersCount;
_ = spContainerProperties.StorageUsed;
int digits = 2;
double value = BytesToGB(spContainerProperties.StorageUsed);
value = Math.Round(value, digits);
StorageUsed = spContainerProperties.StorageUsed;
}

private static double BytesToGB(long value)
{
double num = 1073741824.0;
return (double)value / num;
}
}
}
Loading