Skip to content

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

Description

@kasperbolarsen

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?

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

Activity

  1. jackpoz commented on Jul 21, 2025

    @jackpoz
    Contributor

    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
  2. kasperbolarsen commented on Jul 21, 2025

    @kasperbolarsen
    ContributorAuthor

    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?

  3. siddharth-vaghasia commented on Jul 24, 2025

    @siddharth-vaghasia
    Contributor

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

  4. KoenZomers commented on Aug 20, 2025

    @KoenZomers
    Collaborator

    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.

  5. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions