Add Alternate Access Mapping URL to Host Named Site Collection Zone

Requirement: Add new URL to Host named site collections alternate access mappings.
In a  SharePoint hosting Web application, once sites are provisioned from the hosting control panel, We got to manually add an internet zone URL with “HTTPS” to make it available over the Internet.

Host named site collection alternate access mapping
Host-named site collections are enhanced in SharePoint 2016 to allow you to add multiple URLs by using the Set-SPSite cmdlet.

  • Set-SPSiteUrl – Adds new top-level URLs to a host named site collection
  • Get-SPSiteUrl – Gets the list of all the top-level URLs assigned to a host named site collection.
  • Remove-SPSiteUrl – This enables you to remove a top-level URL assigned to a host named site collection.

How to Add AAM URL to Host Named Site Collection’s Zone?

Here is an example of adding alternate access mapping to a host-named site collection.

$Site = Get-SPSite "https://intranet.crescent.com"
Set-SPSiteURL -Identity $Site -URL "https://portal.crescent.com" -Zone Internet

PowerShell script to Add new URL to internet zone for a host-named site collection:
For my requirement, I had to scan for all host-named site collections to check if the site has a URL mapping for the Internet zone. If not, add a URL to the Internet zone of the Host named site collection.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$OutPutFile = "D:\Scripts\Log.txt"
 
#Get All Host Named Sites of the Hosting Web Application
$SitesColl = Get-SPSite -Limit $All | Where-Object {$_.HostHeaderIsSiteName -eq $true} 

#Loop through each site
ForEach($site in $SitesColl)
{
    #Check if site created in Internet Zone
    if( (Get-SPSiteURL -Identity $site.Url | Where Zone -eq "Internet").count -eq 0)
    {
        Set-SPSiteURL -Identity $site.URL -Zone Internet -Url ($site.Url -replace "http", "https")
        Write-host "Created Internet Zone for site:"$site.Url 

        #Append to a Log
        "Created Internet Zone for site: $($site.Url) on $(get-date)" >> $OutPutFile
    }
}

We scheduled the script to run every 5 minutes using the Windows Task scheduler.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *