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.

  1. 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"

  2. Initialize client context object with the site URL.
    1. $siteURL = ""
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    3. $siteFeatureId = ""

  3. 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.
    1. #Not required for on premise site - Start
    2. $userId = ""
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
    5. $ctx.credentials = $creds
    6. #Not required for on premise site - End

  4. 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

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.

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.

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.

Note: The above set of operations needs to be called to get it executed.

  1. GetSiteCollectionFeatures # Gets all the features from site scope.
  2. GetSiteCollectionFeature # Gets a feature using feature id at site scope.
  3. EnableSiteCollectionFeature # Adds site collection feature.
  4. 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.