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 site policy at 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 "Site Policies" link under "Site Collection Administration" section.
  • Create new policy as per requirement. I've created a policy which 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 "Manage publishing for this policy" link next to the site policy you just created.
  • Choose "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 "Site Policy" Feature, if its not activated already
"Site Policy" Feature must be activated on the site collection level to apply the Policy. If its not activated already (In Group sites, Its 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 "Site Closure and Deletion" link under Site Administration. Select the site policy which you created and Click "OK" button. Come back to "Site Closure and Deletion" page to click "Close this site now" button to close a site collection. The selected site policy 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 "Site Policy" feature at site collection, if its 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.

1 comment:

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

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.