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).
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$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.
# Get Site Scoped features
$features = Get-SPOFeature -Scope Site
Write-Host "Total active site collection features count "
$features.Count foreach($feature in $features){
Write-Host "Feature Name : " $feature.DisplayName
Write-Host "Feature ID : " $feature.DefinitionId
}
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.
# Check whether DocumentSet site feature is active
$feature = Get-SPOFeature -Identity 3bae86a2-776d-499d-9db8-fa4cdc7884f8 -Scope Site
if($feature.DisplayName -ne $null){
Write-Host $feature.DisplayName " feature is active" }
else{
Write-Host "Couldn't find info about feature id. Feature is not active"
}
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).
# Active document id service site feature
Enable-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site $feature = Get-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site
if($feature.DisplayName -ne $null){
Write-Host $feature.DisplayName " feature is activated" }
else{
Write-Host "Feature is not activated" }
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).
# Deactivates document id service site feature using feature id
Disable-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318 -Scope Site
$feature = Get-SPOFeature -Identity b50e3104-6812-424f-a011-cc90e6327318
if($feature.DisplayName -eq $null){
Write-Host "feature is deactivated" }
else{
Write-Host "Feature is still active"
}
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.
ความคิดเห็น