Enable Site and Web Features CSOM PowerShell Script Office 365 SharePoint 2013

Hi All, Today I will be sharing code to activate web and site features using CSOM and PowerShell script. I am following the same structure keeping the input values in XML file and run the script.

PS Code :
cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

function AddFeature([Microsoft.SharePoint.Client.Web] $web)
{
foreach ($FeatureSite in $xmldata.WebSite.FeaturesID.FeatureSite)
{
$featureIdSite = $FeatureSite.ID
$fId = [GUID]$featureIdSite
Enable-Feature $fId $True $FeatureSite.Scope
}

}



function Enable-Feature
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=1)]
[string]$featureId,

[Parameter(Mandatory=$true, Position=2)]
[bool]$force,

[Parameter(Mandatory=$true, Position=3)]
[Microsoft.SharePoint.Client.FeatureDefinitionScope]$featureDefinitionScope
)
Write-Host “Enabling feature $featureId on $featureDefinitionScope” -foregroundcolor black -backgroundcolor yellow

$featureGuid = new-object System.Guid $featureId

$features = $null

if ($featureDefinitionScope -eq [Microsoft.SharePoint.Client.FeatureDefinitionScope]::Site)
{

$features = $clientContext.Site.Features

} else {

$features = $clientContext.Web.Features

}
$clientContext.Load($features)
$clientContext.ExecuteQuery()

$feature = $features.Add($featureGuid, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)

# TODO: Check if the feature is already enabled
$clientContext.ExecuteQuery()

Write-Host “Feature succesfully enabled” -foregroundcolor black -backgroundcolor green
}
AddFeature $web

XML Input Entries-

<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<FeaturesID>
<FeatureSite ID=”f6924d36-2fa8-4f0b-b16d-06b7250180fa” Scope=”Site”>
</FeatureSite>
<FeatureSite ID=”22a9ef51-737b-4ff2-9346-694633fe4416″ Scope=”Web”>
</FeatureSite>
</FeaturesID>
</WebSite>

Read some SharePoint 2013 PowerShell tutorial:


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →