Pages

Wednesday, May 13, 2015

Activate office 365 SharePoint online site feature using Powershell

Due to some reason, I was unable to activate SharePoint publishing feature in one of the office 365 SharePoint site. After I goggled, got the very good link for activating the site collection feature using Power Shell script through SharePoint Management shell.

I have used the below script(copy and save it as .ps1 file and run on it your on promise SharePoint 2013 server which doesn’t required copy the DLL’s) and referred following link which saved my time https://gallery.technet.microsoft.com/office/How-to-enable-a-SharePoint-5bb614c7

Power Shell Script

############################################################################################################################################

#Script that enables a feature in a SPO Site

# Required Parameters:

# -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

# -> $sPassword: Password for the user.

# -> $sSiteColUrl: SharePoint Online Site Collection

# -> $sFeatureGuid: GUID of the feature to be enabled

############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to enable a SPO Feature

function Enable-SPOFeature

{

param ($sSiteColUrl,$sUserName,$sPassword,$sFeatureGuid)

try

{

#Adding the Client OM Assemblies

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

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

#SPO Client Object Model Context

$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)

$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)

$spoCtx.Credentials = $spoCredentials

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green

Write-Host "Enabling the Feature with GUID $sFeatureGuid !!" -ForegroundColor Green

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green

$guiFeatureGuid = [System.Guid] $sFeatureGuid

$spoSite=$spoCtx.Site

$spoSite.Features.Add($sFeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)

$spoCtx.ExecuteQuery()

$spoCtx.Dispose()

}

catch [System.Exception]

{

write-host -f red $_.Exception.ToString()

}

}

#Required Parameters

$sSiteColUrl = "https://ramu.sharepoint.com"

$sUserName = "aramu@ramu.com"

$sFeatureGuid= "f6924d36-2fa8-4f0b-b16d-06b7250180fa"

#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString

$sPassword=convertto-securestring "youraccountpassword" -asplaintext -force

Enable-SPOFeature -sSiteColUrl $sSiteColUrl -sUserName $sUserName -sPassword $sPassword -sFeatureGuid $sFeatureGuid