top of page
Writer's pictureNakkeeran Natarajan

Working With SharePoint Web Scope Features Using CSOM PowerShell

Updated: Mar 29, 2019

In this article, you will learn how retrieve, enable or disable the site level features (web scope) programmatically using CSOM with PowerShell on SharePoint 2013 / SharePoint 2016 / SharePoint online. This article applies to features present on web scope or sub sites level.

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\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


  • Initialize client context object with the site URL.

$siteURL = ""

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  


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

# Credentials for on premise site - Start  

$pwd = Read-Host -Prompt "Enter password" -AsSecureString    

$creds = New-Object System.Net.NetworkCredential("", $pwd)    

$ctx.Credentials = $creds    

# Credentials for on premise site - End 

Get Active Site Level Features (Web Scope):

  • Initialize the web object and get the web property.

  • Get the features from web object using features property.

  • Load the objects and execute query to get the data.

  • Get the feature details individually using for each loop.

  1. For each web feature, use the retrieve method to get the appropriate property value. In this case, feature display name will be retrieved.

  2. Load the web feature object and execute the query. Then access the feature properties. The feature properties retrieved are feature id and feature name.

function GetWebFeatures()

{          

# Access the web         

$web = $ctx.Site.OpenWeb("/SharePoint")          

# Get the features         

$webFeatures = $web.Features          

$ctx.Load($web)          

$ctx.Load($webFeatures)          

$ctx.ExecuteQuery()          

foreach($webFeature in $webFeatures)

{              

# Set the property name to retrieve the value             

$webFeature.Retrieve("DisplayName")              

$ctx.Load($webFeature)              

$ctx.ExecuteQuery()              

Write-Host $webFeature.DefinitionId              

Write-Host $webFeature.DisplayName          

}      }   

Get/Check Site level Feature Properties/Status (Web Scope):

Here we will see how we can get the particular feature and check whether the site feature (web scope) is active or not.

  • Get the feature id from the Microsoft site. The scope of the feature should be web.

  • 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 if else condition, identify whether the site feature is active. If the feature id or name exists from the above object, then the feature is in active mode.

function GetWebFeature()

{          

# Set Feature ID             

$web = $ctx.Site.OpenWeb("/SharePoint")        

$checkFeature = $web.Features.GetById($webFeatureId)          

$checkFeature.Retrieve("DisplayName")          

$ctx.Load($checkFeature)          

$ctx.ExecuteQuery()      

if($checkFeature.DefinitionId -eq $null)

{             

 Write-Host "Web Scope Feature is not active"     

}      

else

{             

 Write-Host $checkFeature.DisplayName "Web Scope Feature is active"     

}      }   

Activate the site level feature (Web Scope):

In the above section, we have seen how we can check the status of the site level feature. Here we will see how we can activate the feature which is already installed on the site.

  • To activate the web 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 web 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 level feature (Web Scope)  

function EnableWebFeature()

{      

try{              

$web = $ctx.Site.OpenWeb("/SharePoint")                    

# Add the feature using feature id and scope.             

$newWebFeature = $web.Features.Add($webFeatureId, 

$true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)              

$newWebFeature.Retrieve("DisplayName")              

$ctx.Load($newWebFeature)              

$ctx.ExecuteQuery()      

if($newWebFeature.DefinitionId -ne $null)

{                  

Write-Host $newWebFeature.DisplayName "Web scoped Feature activated"         

}                  }      

catch

{              

Write-Host "Error in activating web scoped feature : 

$($_.Exception.Message)" -ForegroundColor Red          

}      }   

Deactivate the Site level feature (Web Scope):

In the above section, we have seen how we can activate the site level feature. Now we will see how we can deactivate the site level feature.

  • To de-activate the site level (web) 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 web object and execute the query.

# Deactivate the site level feature (Web Scope)  

function DisableWebFeature(){      

try{              

$web = $ctx.Site.OpenWeb("/SharePoint")              

$web.Features.Remove($webFeatureId, $true)             

 $ctx.Load($web)             

 $ctx.ExecuteQuery()              

Write-Host "Web Feature deactivated"     

}      

catch

{              

Write-Host "Error in deactivating web scoped feature : 

$($_.Exception.Message)"-ForegroundColor Red         

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

# Feature to be activated  

$webFeatureId = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" 

# SharePoint Server Publishing    

GetWebFeatures 

# Gets all the features from web scope.    

GetWebFeature 

# Gets a feature using feature id at web scope.    

EnableWebFeature # Adds web feature.    

DisableWebFeature # Removes web feature.   

0 comments

Comments


bottom of page