Introduction
In this article, you will learn how retrieve, enable or disable the site collection features programmatically using CSOM with PowerShell on SharePoint 2013 / SharePoint online.
Steps Involved
The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.
Add-Type -Path
"C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path
"C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $siteFeatureId = ""
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server or you need to pass the network credentials and set the context.
Get Active Site Collection Features
- Initialize the site object and get the site property.
- Get the features from site object using features property.
- Load the objects and execute query to get the data.
- Get the feature details individually using for each loop.
- For each site collection feature, use the retrieve method to get the appropriate property value. In this case, feature display name will be retrieved.
- Load the site collection feature object and execute the query. Then access the feature properties. The feature properties retrieved are feature id and feature name.
- function GetSiteCollectionFeatures(){
- # Access the site
- $site = $ctx.Site
- # Get the features
- $siteFeatures = $site.Features
- $ctx.Load($site)
- $ctx.Load($siteFeatures)
- $ctx.ExecuteQuery()
-
- foreach($siteFeature in $siteFeatures){
- # Set the property name to retrieve the value
- $siteFeature.Retrieve("DisplayName")
- $ctx.Load($siteFeature)
- $ctx.ExecuteQuery()
- Write-Host $siteFeature.DefinitionId
- Write-Host $siteFeature.DisplayName
- }
- }
Get/Check Site Collection Feature Properties/Status
Here we will see how we can get the particular feature and check whether the site collection feature is active or not.
- Get the feature id from the Microsoft site. The scope of the feature should be site.
- Get the feature using GetById method with feature id.
- Use the retrieve method to get the appropriate property value.
- Load the feature and execute the query.
- Using the if else condition, identify whether the site collection feature is active. If the feature id or name exists from the above object, then the feature is in active mode.
- function GetSiteCollectionFeature(){
- # Set Feature ID
- $siteFeatureId = "b50e3104-6812-424f-a011-cc90e6327318" # Document ID Service feature
- $site = $ctx.Site
- $checkFeature = $site.Features.GetById($siteFeatureId)
- $checkFeature.Retrieve("DisplayName")
- $ctx.Load($checkFeature)
- $ctx.ExecuteQuery()
-
- if($checkFeature.DefinitionId -eq $null){
- Write-Host "Site Feature is not active"
- }
- else{
- Write-Host $checkFeature.DisplayName "Site Feature is active"
- }
- }
Activate the site collection feature
In the above section, we have seen how we can check the status of the site collection feature. Here we will see how we can activate the feature which is already installed on the site.
- To activate the site feature, use the add method method to the features collection. The necessary parameters are feature id, boolean value to force enable the feature and the scope. The scope here will be none, since we are already adding the feature to site object. Boolean value is true to force enable the feature.
- Use the retrieve method to get the appropriate property value.
- Load the feature object and execute the query.
- Check the definition id, if definition id is not null then the site collection feature is activated.
- # Activate the site collection feature
- function EnableSiteCollectionFeature(){
- try{
- $site = $ctx.Site
- # Add the feature using feature id and scope.
- $newSiteFeature = $site.Features.Add($siteFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)
- $newSiteFeature.Retrieve("DisplayName")
- $ctx.Load($newSiteFeature)
- $ctx.ExecuteQuery()
- if($newSiteFeature.DefinitionId -ne $null){
- Write-Host $newSiteFeature.DisplayName "Site Feature activated"
- }
- }
- catch{
- Write-Host "Error in activating site feature : $($_.Exception.Message)" -ForegroundColor Red
- }
- }
Deactivate the Site Collection feature
In the above section, we have seen how we can activate the site collection feature. Now we will see how we can deactivate the site collection feature.
- To de-activate the site collection feature, use the remove method to the features collection. The necessary parameters are feature id, boolean value to force enable the feature. The feature id can be identified from the microsoft blog site (mentioned above). Boolean value is true to force disable the feature.
- Load the site collection object and execute the query.
- # Deactivate the site collection feature
- function DisableSiteCollectionFeature(){
- try{
- $site = $ctx.Site
- $site.Features.Remove($siteFeatureId, $true)
- $ctx.Load($site)
- $ctx.ExecuteQuery()
- Write-Host "Site Feature deactivated"
- }
- catch{
- Write-Host "Error in deactivating feature : $($_.Exception.Message)"-ForegroundColor Red
- }
- }
Note: The above set of operations needs to be called to get it executed.
- GetSiteCollectionFeatures # Gets all the features from site scope.
- GetSiteCollectionFeature # Gets a feature using feature id at site scope.
- EnableSiteCollectionFeature # Adds site collection feature.
- DisableSiteCollectionFeature # Removes site collection feature.
Summary
Thus you have learned how to get retrieve, activate or de-activate the site collection features using CSOM with PowerShell on SharePoint 2013 / SharePoint online sites.