powershell icon indicating copy to clipboard operation
powershell copied to clipboard

Get-PnPHubSite doesn't return NULL then the specified URL or Id points to a site that is not a hub site

Open kasperbolarsen opened this issue 9 months ago • 4 comments

Actual behavior

Get-PnPHubSite currently returns a not-null respons when calling using a valid URL to a site that is not a hub site. I would expect a null value

Steps to reproduce behavior

part of an Azure Function: $SPOAdminUrl = "the URL of the SP admin site" $siteUrl = "contoso.sharepoint.com/sites/nothubsite" $adminConn = Connect-PnPOnline -Url $SPOAdminUrl -Interactive -ClientId $PnPClientId -ReturnConnection $hubSite = Get-PnPHubSite -Identity $siteUrl -Connection $adminConn -ErrorAction Stop if($hubSite) #this fails as $hubSite is not null { $body += "The site $siteUrl is already registered as a hub site" $StatusCode = [HttpStatusCode]::AlreadyReported }

What is the version of the Cmdlet module you are running?

3.1.0

Which operating system/environment are you running PnP PowerShell on?

  • [X] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

kasperbolarsen avatar Jul 21 '25 07:07 kasperbolarsen

The returned objects is from CSOM, as a workaround you can check $hubSite.ServerObjectIsNull as property instead which is $true when the site is not a hubsite:

Image

jackpoz avatar Jul 21 '25 08:07 jackpoz

I hear you, but that smells like code rot to me. I might be wrong, but I would expect the generel rule to be that commands should return a null in cases like this?

kasperbolarsen avatar Jul 21 '25 09:07 kasperbolarsen

@kasperbolarsen - agree, a simple condition in code can handle this, I can take this up...

siddharth-vaghasia avatar Jul 24 '25 08:07 siddharth-vaghasia

Tricky one. I hear what you are saying @kasperbolarsen, but as @jackpoz indicates, this is how SharePoint returns it. PnP is just passing it on. I would agree with you that returning a null response would be cleaner and we certainly can fix things in PnP where SharePoint itself falls short, but.. changing this will break any current implementation that people might be using today that does rely on one of the various alternative options that will work, such as the $hubSite.ServerObjectIsNull or a if($hubSite.SiteId) instead of if($hubSite). We therefore can't change this now, risking breaking things for other people, especially since the alternative is so simple. I will mark it to be considered for a v4 release, but that won't happen anytime soon, likely not this calendar year at least.

KoenZomers avatar Aug 20 '25 23:08 KoenZomers