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

Feature: Improved storage entity code #2275

Merged
merged 2 commits into from
Aug 27, 2022
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- `Get-PnPAvailableSensitivityLabel` cmdlet now uses the non-deprecated Graph API to retrieve sensitivity label. [#2234](https://github.com/pnp/powershell/pull/2234)
- Improved `Get-PnPMicrosoft365Group` cmdlet to better check the Id, DisplayName and MailNickname of Microsoft 365 Group. [#2258](https://github.com/pnp/powershell/pull/2258)

- Improved `Get-PnPStorageEntity` cmdlet when `Key` parameter is specified. [#2275](https://github.com/pnp/powershell/pull/2275)
### Removed

### Fixed
Expand Down
74 changes: 51 additions & 23 deletions src/Commands/Admin/GetStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Linq;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using System.Collections.Generic;
using System.Text.Json;
Expand All @@ -22,33 +19,71 @@ public class GetPnPStorageEntity : PnPSharePointCmdlet
protected override void ExecuteCmdlet()
{
string storageEntitiesIndex = string.Empty;
if (Scope == StorageEntityScope.Tenant)

if (Scope == StorageEntityScope.Site)
{
var appCatalogUri = ClientContext.Web.GetAppCatalog();
if (appCatalogUri != null)
var appcatalog = ClientContext.Site.RootWeb.SiteCollectionAppCatalog;
ClientContext.Load(appcatalog);
ClientContext.ExecuteQueryRetry();
if (appcatalog.ServerObjectIsNull == false)
{
using (var clonedContext = ClientContext.Clone(appCatalogUri))
if (ParameterSpecified(nameof(Key)))
{
var storageEntity = ClientContext.Site.RootWeb.GetStorageEntity(Key);
ClientContext.Load(storageEntity);
ClientContext.ExecuteQueryRetry();

var storageEntityValue = new StorageEntity
{
Key = Key,
Value = storageEntity.Value,
Comment = storageEntity.Comment,
Description = storageEntity.Description
};
WriteObject(storageEntityValue);
}
else
{
storageEntitiesIndex = clonedContext.Web.GetPropertyBagValueString("storageentitiesindex", "");
storageEntitiesIndex = ClientContext.Site.RootWeb.GetPropertyBagValueString("storageentitiesindex", "");
}
}
else
{
WriteWarning("Tenant app catalog is not available on this tenant.");
WriteWarning("Site Collection App Catalog is not available on this site.");
}
}
else
{
var appcatalog = ClientContext.Site.RootWeb.SiteCollectionAppCatalog;
ClientContext.Load(appcatalog);
ClientContext.ExecuteQueryRetry();
if (appcatalog.ServerObjectIsNull == false)
var appCatalogUri = ClientContext.Web.GetAppCatalog();
if (appCatalogUri != null)
{
storageEntitiesIndex = ClientContext.Site.RootWeb.GetPropertyBagValueString("storageentitiesindex", "");
using (var clonedContext = ClientContext.Clone(appCatalogUri))
{
if (ParameterSpecified(nameof(Key)))
{
var storageEntity = clonedContext.Site.RootWeb.GetStorageEntity(Key);
clonedContext.Load(storageEntity);
clonedContext.ExecuteQueryRetry();

var storageEntityValue = new StorageEntity
{
Key = Key,
Value = storageEntity.Value,
Comment = storageEntity.Comment,
Description = storageEntity.Description
};

WriteObject(storageEntityValue);
}
else
{
storageEntitiesIndex = clonedContext.Web.GetPropertyBagValueString("storageentitiesindex", "");
}
}
}
else
{
WriteWarning("Site Collection App Catalog is not available on this site.");
WriteWarning("Tenant app catalog is not available on this tenant.");
}
}

Expand All @@ -68,14 +103,7 @@ protected override void ExecuteCmdlet()
};
storageEntities.Add(storageEntity);
}
if (ParameterSpecified(nameof(Key)))
{
WriteObject(storageEntities.Where(k => k.Key == Key));
}
else
{
WriteObject(storageEntities, true);
}
WriteObject(storageEntities, true);
}
}
}
Expand Down