In this post, you will learn, how we can retrieve, activate or deactivate Web scoped features on SharePoint site or sub sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.
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 following operations will be compatible with SharePoint 2013 On Premise and Office 365 versions.
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).
Since, we are working with the features with Web Scope, the site URL can be the site collection or sub sites available. Once connected, you can carry out any of the operations mentioned below, based on the requirement.
Retrieve Web Scope Features:
The Web scoped features active on the site or sub sites are retrieved in the operation, given below. Get-SPOFeature command is used to get all the features from the site. Since, we are retrieving the Web scoped features, the scope parameter is not required in this operation.
Each and every feature information is retrieved, using for each loop. The display name and definition Id are retrieved from the features. The code snippet given below helps retrieving all the Web scoped features from the site or sub site, specified in the context.
The feature can be retrieved, using the identity. The identity can be the feature name or Id. The scope parameter is optional. Though, Web is passed as a scope. The following code snippet retrieves the feature, using the feature Id.
Enable Web Scope Feature:
The feature can be activated, using PnP PowerShell script. Enable-SPOFeature command is used to activate the features. Along with the command, the necessary parameter is the identity. The identity is the feature Id. The scope is an optional parameter.
The code snippet, given below, helps in activating the feature at the Web scope.
The image, given below, shows the activated feature. The URL is required to access the Web scope feature.
Disable Web Scope 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 parameter is the identity. The identity is the feature Id. The scope is an optional parameter.
The code snippet, given below, helps in deactivating the feature at the Web scope.
Note: The operations will be compatible for SharePoint 2013/2016 On Premise / SharePoint online sites.