kwizcom banner advertisement

SharePoint Online: Activate Feature using PowerShell

Requirement: Activate a feature in SharePoint online site collection using PowerShell.

Solution: In SharePoint On-Premises, We use: Enable-SPFeature cmdlet to activate/enable features. In SharePoint online there is no such cmdlet available through PowerShell to activate feature. But we can utilize Client Side Object Model (CSOM) to activate feature in SharePoint online. Lets activate "SharePoint Server Publishing Infrastructure" feature
Make sure you have SharePoint online Client SDK installed on your client machine to use this code. You can download it from:

SharePoint online: PowerShell script to enable feature
Lets activate sharepoint online feature using PowerShell
#Load SharePoint CSOM Assemblies
Import-Module Microsoft.Online.SharePoint.Powershell

#Variables for Processing
$SiteURL = ""
$FeatureGUID =[System.GUID]("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature ID
$LoginName =""
$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 = $

#sharepoint online powershell activate feature
$site.Features.Add($FeatureGUID, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::farm)    

write-host "Feature has been Activated!" 

SharePoint Online: PowerShell to Activate Feature
Lets Add some error handling to the above code such as "check if feature is activated already" and create a reusable function to the PowerShell to enable feature 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)
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollURL)
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
        $Ctx.Credentials = $Credentials

        #Check the Feature Status
        $FeatureStatus =  $Site.Features.GetById($FeatureGuid)

        #Activate the feature 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
            Write-Host "Feature Enabled on site $SiteCollURL!" -ForegroundColor Green
            Write-host "Feature is Already Active on the Site collection!" -ForegroundColor Red
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
#Parameters to Activate Feature
$SiteCollURL = ""
$UserName = ""
$Password = "Password goes here"
$FeatureGuid= [System.Guid] ("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature
$SecurePassword= ConvertTo-SecureString $Password –asplaintext –force  

#Enable Feature
Enable-SPOFeature -SiteCollURL $SiteCollURL -UserName $UserName -Password $SecurePassword -FeatureGuid $FeatureGuid
This activates SharePoint Server Publishing Infrastructure feature in SharePoint online.
sharepoint online activate publishing feature powershell
How to Activate Site Feature (Web) using PowerShell?
While the above code activates site collection level feature, the same code can be used to activate any web level feature also. Simply change the line:
Also set the GUID for web level feature!This activates feature using PowerShell in SharePoint online.
SharePoint Online: Activate Feature using PowerShell SharePoint Online: Activate Feature using PowerShell Reviewed by Salaudeen Rajack on January 03, 2015 Rating: 5


Please Login and comment to get your questions answered!

Powered by Blogger.