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

Bufix Get-PnPSiteCollectionAppCatalog throws error for deleted sites #2201

Merged
merged 3 commits into from
Jul 29, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed issue where passing in `-Connection` to `Disconnect-PnPOnline` would throw an exception [#2093](https://github.com/pnp/powershell/pull/2093)
- Fixed `Get-PnPSiteSearchQueryResults` throwing `Value cannot be null` exception [#2138](https://github.com/pnp/powershell/pull/2138)
- Fixed `New-PnPUPABulkImportJob` not returing the job Id [#2144](https://github.com/pnp/powershell/pull/2144)
- Fixed `Get-PnPSiteCollectionAppCatalog` throwing an exception when the site was deleted [#2201](https://github.com/pnp/powershell/pull/2201)

### Contributors
- Milan Holemans [milanholemans]
- Miguel A. Tena [mikewaretena]
- Reshmee Auckloo [reshmee011]
- Leon Armston [LeonArmston]
Expand Down
51 changes: 30 additions & 21 deletions src/Commands/Admin/GetSiteCollectionAppCatalog.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Model.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace PnP.PowerShell.Commands
{
Expand All @@ -16,21 +16,22 @@ namespace PnP.PowerShell.Commands
public class GetSiteCollectionAppCatalog : PnPAdminCmdlet
{
[Parameter(Mandatory = false)]
public SwitchParameter ExcludeDeletedSites;
public SwitchParameter ExcludeDeletedSites;

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

protected override void ExecuteCmdlet()
{
WriteVerbose("Retrieving all site collection App Catalogs from SharePoint Online");

var appCatalogsCsom = ClientContext.Web.TenantAppCatalog.SiteCollectionAppCatalogsSites;
ClientContext.Load(appCatalogsCsom);
ClientContext.ExecuteQueryRetry();
ClientContext.ExecuteQueryRetry();

var appCatalogsLocalModel = appCatalogsCsom.Select(ac =>
new SiteCollectionAppCatalog {
new SiteCollectionAppCatalog
{
AbsoluteUrl = ac.AbsoluteUrl,
ErrorMessage = ac.ErrorMessage,
SiteID = ac.SiteID
Expand All @@ -39,7 +40,7 @@ protected override void ExecuteCmdlet()

WriteVerbose($"{appCatalogsLocalModel.Count} site collection App Catalog{(appCatalogsLocalModel.Count != 1 ? "s have" : " has")} been retrieved");

if(CurrentSite.ToBool())
if (CurrentSite.ToBool())
{
SiteContext.Site.EnsureProperties(s => s.Id);

Expand All @@ -48,7 +49,7 @@ protected override void ExecuteCmdlet()

appCatalogsLocalModel.Clear();

if(currentSite == null)
if (currentSite == null)
{
WriteVerbose($"Current site at {Connection.Url} with ID {SiteContext.Site.Id} does not have a site collection App Catalog on it");
return;
Expand All @@ -66,21 +67,29 @@ protected override void ExecuteCmdlet()
{
WriteVerbose($"Validating site collection App Catalog at {appCatalogLocalModel.AbsoluteUrl}");

// Deleted sites throw either an exception or return null
appCatalogLocalModel.AbsoluteUrl = Tenant.GetSitePropertiesById(appCatalogLocalModel.SiteID.Value, false).Url;
results.Add(appCatalogLocalModel);
}
catch (Microsoft.SharePoint.Client.ServerException e) when (e.ServerErrorTypeName.Equals("Microsoft.Online.SharePoint.Common.SpoNoSiteException", StringComparison.InvariantCultureIgnoreCase))
{
if(!ExcludeDeletedSites.ToBool())
{
WriteVerbose($"Site collection App Catalog at {appCatalogLocalModel.AbsoluteUrl} regards a site that has been deleted");
results.Add(appCatalogLocalModel);
}
else
catch (Exception e)
{
if (e is NullReferenceException || (e is ServerException se && se.ServerErrorTypeName.Equals("Microsoft.Online.SharePoint.Common.SpoNoSiteException", StringComparison.InvariantCultureIgnoreCase)))
{
WriteVerbose($"Site collection App Catalog at {appCatalogLocalModel.AbsoluteUrl} regards a site that has been deleted. Since the {nameof(ExcludeDeletedSites)} flag has been provided, it will not be included in the results.");
if (!ExcludeDeletedSites.ToBool())
{
WriteVerbose($"Site collection App Catalog at {appCatalogLocalModel.AbsoluteUrl} regards a site that has been deleted");
results.Add(appCatalogLocalModel);
}
else
{
WriteVerbose($"Site collection App Catalog at {appCatalogLocalModel.AbsoluteUrl} regards a site that has been deleted. Since the {nameof(ExcludeDeletedSites)} flag has been provided, it will not be included in the results.");
}

continue;
}
}

throw;
}
}
}

Expand Down