Monday, December 26, 2016

Activate SharePoint Online features by using PowerShell/CSOM

Using below script we can activate the feature in sharepoint online site

############################################################################################################################################
# Required Parameters:
#  -> $UserName: User Name to connect to the SharePoint Online Site
#  -> $sPass: Password for the user.
#  -> $SiteUrl: SharePoint Online Site Collection
#  -> $FeatureGuid: GUID of the feature to be enabled
############################################################################################################################################

#Definition of the function that allows to enable a SPO Feature
function Activate-SPOFeature
{
    param ($SiteUrl,$UserName,$sPass,$FeatureGuid)
    try
    {    
        #Adding the Client OM Assemblies        
        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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

  $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$sPass)
if (!$context.ServerObjectIsNull.Value)
{
   #Write-Host "Connected to SharePoint Online site: '$SubSiteURL'" -ForegroundColor green
#Add-Content -Path $LogFileName -Value([string]::Concat("Connected to SharePoint Online site:",$url))
}
$context.Credentials = $Creds
$checkAlertEmailFeature = $context.Web.Features.GetById($FeatureGuid)
   $checkAlertEmailFeature.Retrieve("DisplayName")
   $context.Load($checkAlertEmailFeature)
   $context.ExecuteQuery()
   
   if($checkAlertEmailFeature.DefinitionId -eq $null){
       Write-Host "Alert Notification  Feature need to active"  -ForegroundColor Green
$newSiteFeature = $context.Web.Features.Add($FeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::Site)
$context.Load($newSiteFeature)
$context.ExecuteQuery()
if($newSiteFeature.DefinitionId -ne $null)
{
          Add-Content -Path $LogFileName -Value([string]::Concat("Alert Notification feature activated"))
Write-Host "Alert Notification feature activated"  -ForegroundColor Green
      }
else
{
Write-Host "Alert Notification feature not yet activated"  -ForegroundColor Green
Add-Content -Path $LogFileName -Value([string]::Concat("Alert Notification feature not yet activated"))
}
   }
   else
{
       Add-Content -Path $LogFileName -Value([string]::Concat("Alert Notification feature activated"))
   }
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()  
    }    
}

#Required Parameters
$SiteUrl = "https://*****.sharepoint.com/Subsite"
$UserName = "Naga@contoso.com"
$FeatureGuid= "ab2e9a62-d1b3-4f47-9678-7e7a72e1c645"
#$sPass = Read-Host -Prompt "Enter your password: " -AsSecureString  
$sPass=convertto-securestring "******" -asplaintext -force

Activate-SPOFeature -sSiteColUrl $SiteUrl -sUserName $UserName -sPassword $sPass -sFeatureGuid $FeatureGuid

Happy code.. :)

No comments:

Post a Comment