Working With SharePoint Site Scoped Features Using PnP PowerShell

Introduction

In this article, we will learn how we can retrieve, activate, or deactivate site scoped features on SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite

You need to have PowerShell 3.0 available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers, or view more documentation on the official site. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The PnP PowerShell is supported by SharePoint 2013 On Premise and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.

Connect To Site

Connect to the site using the snippet given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).

  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  

Since we are working with the features with site Scope, the site URL should be site collection only. Once connected, you can carry out any of the operations mentioned below, based on the requirement.

The URL to access the site collection features manually is  http://sitecollectionurl/_layouts/15/ManageFeatures.aspx?Scope=Site. 
 
The SharePoint OOB features list can be accessed from here (MSDN site).  

Retrieve Site Scoped Features

The site scoped features active on the site collection are retrieved in the operation given below.

  • Get-SPOFeature command is used to get all the features from the site.
  • The scope parameter is mandatory to get site collection features in this operation.

Each and every feature's information is retrieved using for each loop. The display name and definition Id are retrieved from the features.

The code snippet given below helps in retrieving all the site scoped features from the site specified in the context.
  1. # Get Site Scoped features  
  2. $features = Get-SPOFeature -Scope Site  
  3. Write-Host "Total active site collection features count " $features.Count  
  4. foreach($feature in $features){  
  5.     Write-Host "Feature Name : " $feature.DisplayName  
  6.     Write-Host "Feature ID   : " $feature.DefinitionId  
  7. }  

The feature can be retrieved using the identity. The identity can be the feature name or Id. The following code snippet retrieves the site feature, using the feature Id.

  1. # Check whether DocumentSet site feature is active  
  2. $feature = Get-SPOFeature -Identity 3bae86a2-776d-499d-9db8-fa4cdc7884f8 -Scope Site  
  3. if($feature.DisplayName -ne $null){  
  4.     Write-Host $feature.DisplayName " feature is active"          
  5. }  
  6. else{  
  7.     Write-Host "Couldn't find info about feature id. Feature is not active"  
  8. }   

Enable Site Scoped Feature

The feature can be activated using PnP PowerShell script.

  • Enable-SPOFeature command is used to activate the features. 
  • Along with the command for site features (site collection), the necessary parameters are identity and scope. 
  • The identity value is the site feature Id. The scope value should be site.

 The code snippet, given below, helps in activating the feature on the site collection (at the site scope).

  1. # Active document id service site feature  
  2. Enable-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site  
  3.   
  4. $feature = Get-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site  
  5. if($feature.DisplayName -ne $null){  
  6.     Write-Host $feature.DisplayName " feature is activated"  
  7. }  
  8. else{  
  9.     Write-Host "Feature is not activated"  
  10. }  

The image, given below, shows the activated feature,


Disable Site Scoped Feature

The feature can be deactivated, using PnP PowerShell script.

  • Disable-SPOFeature command is used to deactivate the features. 
  • Along with the command, the necessary parameters are identity and scope in the identity. The identity value is the site feature Id. The scope value is site.

The code snippet, given below, helps in deactivating the feature on site collection (at the site scope).

  1. # Deactivates document id service site feature using feature id  
  2. Disable-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site  
  3.   
  4. $feature = Get-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318  
  5. if($feature.DisplayName -eq $null){  
  6.     Write-Host "feature is deactivated"  
  7. }  
  8. else{  
  9.     Write-Host "Feature is still active"  
  10. }  

Summary

Thus, you have learned how to retrieve the site scoped features from the site and about enabling or disabling the site scoped features, using PnP PowerShell. PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.