diff --git a/CHANGELOG.md b/CHANGELOG.md index f755c99cd..c158d9306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/Commands/Admin/GetSiteCollectionAppCatalog.cs b/src/Commands/Admin/GetSiteCollectionAppCatalog.cs index ede1383d2..460f0dc1e 100644 --- a/src/Commands/Admin/GetSiteCollectionAppCatalog.cs +++ b/src/Commands/Admin/GetSiteCollectionAppCatalog.cs @@ -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 { @@ -16,10 +16,10 @@ 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() { @@ -27,10 +27,11 @@ protected override void ExecuteCmdlet() 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 @@ -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); @@ -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; @@ -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; + } } }