kwizcom banner advertisement

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 hosting control panel, We got to manually add a internet zone URL with "https" to make it available over 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, check if the site has a URL mapping for Internet zone, if not add a URL to 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
    }
 }
Scheduled the script to run for every 5 minutes using Windows Task scheduler.
Add Alternate Access Mapping URL to Host Named Site Collection Zone Add Alternate Access Mapping URL to Host Named Site Collection Zone Reviewed by Salaudeen Rajack on 12:52 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.