SharePoint Online: How to Apply Site Policy using PowerShell?

Requirement: Apply site policy in SharePoint Online using PowerShell.

How to Apply SharePoint Online Site Policy using PowerShell?

Site Policies is a great way to implement governance in SharePoint Online. As of today, we can’t create a new site policy through PowerShell! However, we can save time by creating a site policy at the content type hub instead of creating at each individual site collection. Here is how to create a site policy in SharePoint Online content type hub:

  • Login to the content type hub of your tenant at: https://<tenant>.sharepoint.com/sites/contentTypeHub
  • Click on Settings >> Site Settings
  • Click on the “Site Policies” link under the “Site Collection Administration” section.
  • Create a new policy as per requirement. I’ve created a policy that makes the site collection to ready only by selecting “Do not close or delete site automatically” and check the “The site collection will be read-only when it is closed”.
    sharepoint online create site policy powershell
  • Once the site policy is created,  You have to publish it to the site collections. From the Site Policies page of the content type hub, click on the “Manage publishing for this policy” link next to the site policy you just created.
  • Choose the “Publish” or “Republish” option to push your site policy to all SharePoint Online site collections.

Once you published, It takes sometime for the policy to appear in site collections. You may have to wait up 24 hours. Once your site policy is published, you should see it in the target site collection’s “Site Policy” page.

Activate the “Site Policy” Feature, if it’s not activated already

“Site Policy” Feature must be activated on the site collection level to apply the Policy. If it’s not activated already (In Group sites, It’s disabled by default!), follow these steps:

  • Go to Settings >> Site Settings >> Click on “Site collection features” link.
  • Click on “Active” button for Site Policy Feature.
    sharepoint online site policy feature

Apply Site Policy to the Site Collection

Applying a site policy using the web interface is quite simple. To apply the site policy, Go to Site Setting, click on the “Site Closure and Deletion” link under Site Administration. Select the site policy which you created and click the “OK” button. Come back to the “Site Closure and Deletion” page to click the “Close this site now” button to close a site collection. The selected site policy will be applied after a site collection is closed.

PowerShell to Apply Site Policy to Site Collection:

Alright, When you need to apply a site policy for a large number of sites, we can use PowerShell instead of web UI.  Once the site policy is available in target sites, we can apply it to the site collection through PowerShell. Here is the CSOM PowerShell script to apply:

#Load SharePoint Online CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.Office.Client.Policy.dll"

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PolicyName = "Set Site Read-Only"

#Setup Credentials to connect
$Cred = Get-Credential
     
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Get Site Policies
$Policies = [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::GetProjectPolicies($Ctx, $Web)
$Ctx.Load($Policies)
$Ctx.ExecuteQuery()

#Get the Policy to Apply 
$SitePolicy =  $Policies | Where -Property Name -eq $PolicyName
If($SitePolicy)
{
    #Apply the policy
    [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::ApplyProjectPolicy($Ctx, $Web, $SitePolicy)
    $Web.Update()
    $Ctx.ExecuteQuery()
    
    #Close the Site
    [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::CloseProject($Ctx,$Web)
    $Ctx.ExecuteQuery()

    Write-host "Site Policy has been Applied & Site Closed Successfully!" -f Green
}

PnP PowerShell to Apply Site Policy in SharePoint Online

Now, let’s use PnP PowerShell to activate the “Site Policy” feature at site collection if it’s not activated already, and then apply the “Site Policy” to the given site collection.

#Apply Site Policy for a Site collection
Function Apply-PnPSitePolicy([String]$SiteUrl, [String]$PolicyName)
{
    #Connect to the Site 
    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    #Check if "Site Policy" Feature is active
    $SitePolicyFeature = Get-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site -Web $SiteUrl
    If($SitePolicyFeature.DefinitionId -eq $null)
    {
        #Activate "Site Policy" Feature for the site collection
        Enable-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site
        Write-Host "Site Policy Feature is Activated at $($SiteUrl)" -ForegroundColor Green
    }
    
    #Get Policy to Activate
    $SitePolicyToActivate = Get-PnPSitePolicy -Name $PolicyName | Select-Object -Property Name
    If ($SitePolicyToActivate)
    {
        #Apply Site Policy
        Set-PnPSitePolicy -Name $PolicyName

        #Close the site
        Set-PnPSiteClosure -State Closed

        Write-Host "Site Policy Applied to $($SiteUrl)" -ForegroundColor Green
    }
    Else
    {
        write-Host "Site Policy '$($PolicyName)' not found in Site $($SiteUrl)" -ForegroundColor Yellow
    }
}

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Purchase"
$PolicyName= "Set Site Read-Only"

#Call the function to apply site policy
Apply-PnPSitePolicy -SiteUrl $SiteURL -PolicyName $PolicyName

If you just activated the “Site Policy” feature, policies created in content type hub won’t immediately show at site collections! You have may have to wait up to 24 hours.

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

One thought on “SharePoint Online: How to Apply Site Policy using PowerShell?

  • June 17, 2020 at 6:29 AM

    Hi – Do you have a script for the same for On-Prem as well?

    Reply

Leave a Reply