SharePoint Online: Activate a Feature using PowerShell
Requirement: Activate a feature in SharePoint Online site collection using PowerShell.
Solution: In SharePoint On-Premises, We use the Enable-SPFeature cmdlet to activate/enable features. In SharePoint Online, there is no such cmdlet available through PowerShell to activate the feature. But we can utilize the Client-Side Object Model (CSOM) to activate features in SharePoint Online. Let’s activate the “SharePoint Server Publishing Infrastructure” feature. Here is an example of SharePoint Online PowerShell to activate a site collection feature:
How to Enable a Feature in SharePoint Online?
Activating a feature makes the functionality associated with the feature available. It could bring new functionality or artifacts like lists and libraries, etc. Here is how to activate a site feature in SharePoint Online:
- Navigate to the site where the feature needs to be activated.
- On the site’s home page, click the Settings gear icon and then the Site Settings option from the Settings menu.
- On the Site Settings page, click the “Manage Site Features” link under the Site Actions section.
- Say, for example, here is how to activate the publishing feature in SharePoint Online: On the Site Features page, click on the Activate button next to the site feature you desire to activate.
- The feature status is changed to Active and the associated feature capabilities are enabled on the site.
PowerShell to Enable Site Collection Feature in SharePoint Online
Do you need to enable a feature in SharePoint Online quickly? PowerShell can help enable a feature in SharePoint Online without having to use the web browser GUI! Let me show you how to use PowerShell to enable a feature in SharePoint Online:
#Load SharePoint CSOM Assemblies
Import-Module Microsoft.Online.SharePoint.Powershell
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$FeatureGUID =[System.GUID]("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature ID
$LoginName ="[email protected]"
$LoginPassword ="Password"
#Get the Client Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
#Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword -asplaintext -force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $LoginName, $SecurePWD
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName,$Credential.Password)
#Get the Site
$site = $ctx.site
#sharepoint online powershell activate feature
$site.Features.Add($FeatureGUID, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::farm)
$ctx.ExecuteQuery()
write-host "Feature has been Activated!"
This PowerShell enables publishing features in SharePoint Online.
SharePoint Online: PowerShell to Activate Feature
Let’s add some error handling to the above code, such as “check if the feature is activated already” and create a reusable function in PowerShell to enable the feature in SharePoint Online.
#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"
#Function to Enable Feature in SharePoint Online
Function Enable-SPOFeature
{
param ($SiteCollURL,$UserName,$Password,$FeatureGuid)
Try
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollURL)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
$Ctx.Credentials = $Credentials
$Site=$Ctx.Site
#Check the Feature Status
$FeatureStatus = $Site.Features.GetById($FeatureGuid)
$FeatureStatus.Retrieve("DefinitionId")
$Ctx.Load($FeatureStatus)
$Ctx.ExecuteQuery()
#sharepoint online activate feature using powershell (if its not enabled already)
if($FeatureStatus.DefinitionId -eq $null)
{
Write-Host "Enabling Feature $FeatureGuid..." -ForegroundColor Yellow
$Site.Features.Add($FeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
$Ctx.ExecuteQuery()
Write-Host "Feature Enabled on site $SiteCollURL!" -ForegroundColor Green
}
else
{
Write-host "Feature is Already Active on the Site collection!" -ForegroundColor Red
}
}
Catch
{
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
}
#Parameters to Activate Feature
$SiteCollURL = "https://Crescent.sharepoint.com/sites/Demo"
$UserName = "[email protected]"
$Password = "Password goes here"
$FeatureGuid= [System.Guid] ("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature
$SecurePassword= ConvertTo-SecureString $Password -asplaintext -force
#sharepoint online enable feature powershell
Enable-SPOFeature -SiteCollURL $SiteCollURL -UserName $UserName -Password $SecurePassword -FeatureGuid $FeatureGuid
This activates the SharePoint Server Publishing Infrastructure feature in SharePoint Online.
How to Activate Site Feature (Web) using PowerShell?
While the above code activates the site collection level feature, the same code can be used to activate any web level feature as well. Change the line:
$Site=$Ctx.site
To
$Site=$Ctx.web
Also, set the GUID for web level feature! This activates the feature using PowerShell in SharePoint Online.
PnP PowerShell to Enable Publishing Feature in SharePoint Online:
Let’s activate a SharePoint Online feature using PnP PowerShell at the site collection level using Enable-PnPFeature cmdlet.
#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Procurement"
$FeatureId = "f6924d36-2fa8-4f0b-b16d-06b7250180fa" #Site Scoped Publishing Feature
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#get the Feature
$Feature = Get-PnPFeature -Scope Site -Identity $FeatureId
#Get the Feature status
If($Feature.DefinitionId -eq $null)
{
#sharepoint online powershell enable feature
Write-host -f Yellow "Activating Feature..."
Enable-PnPFeature -Scope Site -Identity $FeatureId -Force
Write-host -f Green "Feature Activated Successfully!"
}
Else
{
Write-host -f Yellow "Feature is already active!"
}
Similarly, you can activate the publishing feature using PnP PowerShell at web scope as:
#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Procurement"
$FeatureId = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" #Web Scoped Publishing Feature
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Feature to activate
$Feature = Get-PnPFeature -Scope Web -Identity $FeatureId
#Get the Feature status
If($Feature.DefinitionId -eq $null)
{
#activate publishing feature sharepoint online using powershell
Write-host -f Yellow "Activating Feature..."
Enable-PnPFeature -Scope Web -Identity $FeatureId -Force
Write-host -f Green "Feature Activated Successfully!"
}
Else
{
Write-host -f Yellow "Feature is already active!"
}
To disable a feature in SharePoint Online using PowerShell, refer: SharePoint Online: Deactivate Feature using PowerShell
Will this apply to Sub-sites as well?
No! This script enables the feature only at the given site collection or subsite. To Activate a feature on all sites, use: How to Enable a Feature on All Sites in SharePoint Online using PowerShell?”
How to deactivate SharePoint Online feature using Powershell?
Here you go: SharePoint Online: Deactivate Feature using PowerShell
Thank you very much for your help. This tutorial is great!
I’m getting a Exception at line 11 ctx.ExecuteQuery()
Exception calling “ExecuteQuery” with “0” argument(s): “Specified method is not supported.”
At line:11 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServerException
What do you think this could be?
How do you find the feature guid id in SPO?
Here you go: How to Find the Feature GUID in SharePoint?
What changes will be applicable for SharePoint on premises.
Here you go: How to Activate a Feature in SharePoint using PowerShell