PowerShell script to activate publishing feature in a Team site (Classic)

%3CLINGO-SUB%20id%3D%22lingo-sub-204865%22%20slang%3D%22en-US%22%3EPowerShell%20script%20to%20activate%20publishing%20feature%20in%20a%20Team%20site%20(Classic)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-204865%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20come%20across%20a%26nbsp%3Bstrange%26nbsp%3Bissue%20when%20using%20the%20PowerShell%20Script%26nbsp%3Busing%20CSOM%20for%20activating%20Publishing%20feature%20in%20SharePoint%20Online%20site%20collection.%20When%20i%20activated%20using%20the%20below%20script%20-%20I%26nbsp%3Bsee%20that%20the%20Pages%20%26amp%3B%20Images%20library%20are%20not%20getting%20created.%20Can%20anyone%20help%20in%20finding%20out%20the%20issue%20here.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EInput%20CSV%20passed%20with%20feature%20ids%20-%3C%2FP%3E%3CTABLE%20border%3D%220%22%20cellspacing%3D%220%22%20cellpadding%3D%220%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3ESite%3C%2FTD%3E%3CTD%3Ef6924d36-2fa8-4f0b-b16d-06b7250180fa%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3EWeb%3C%2FTD%3E%3CTD%3E94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Activate%20the%20Site%20Collection%20Feature%3CBR%20%2F%3Efunction%20EnableSiteCollectionFeature()%7B%3CBR%20%2F%3Etry%7B%3CBR%20%2F%3E%24site%20%3D%20%24ctx.Site%3CBR%20%2F%3E%23Check%20the%20Feature%20Status%3CBR%20%2F%3E%24FeatureStatus%20%3D%20%24site.Features.GetById(%24siteFeatureId)%3CBR%20%2F%3E%24FeatureStatus.Retrieve(%22DefinitionId%22)%3CBR%20%2F%3E%24ctx.Load(%24FeatureStatus)%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3CBR%20%2F%3E%3CBR%20%2F%3E%23Activate%20the%20feature%20if%20its%20not%20enabled%20already%3CBR%20%2F%3Eif(%24FeatureStatus.DefinitionId%20-eq%20%24null)%20%7B%3CBR%20%2F%3E%23%20Add%20the%20feature%20using%20feature%20id%20and%20scope.%3CBR%20%2F%3E%24newSiteFeature%20%3D%20%24site.Features.Add(%24siteFeatureId%2C%20%24true%2C%20%5BMicrosoft.SharePoint.Client.FeatureDefinitionScope%5D%3A%3ANone)%3CBR%20%2F%3E%24newSiteFeature.Retrieve(%22DisplayName%22)%3CBR%20%2F%3E%24ctx.Load(%24newSiteFeature)%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3CBR%20%2F%3Eif(%24newSiteFeature.DefinitionId%20-ne%20%24null)%7B%3CBR%20%2F%3EWrite-Host%20%24newSiteFeature.DisplayName%20%22Site%20Collection%20Feature%20activated%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%7B%3CBR%20%2F%3EWrite-Host%20%22Error%20in%20activating%20site%20Collection%20feature%20-%20%24siteURL%20%3A%20%24(%24_.Exception.Message)%22%20-ForegroundColor%20Red%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%23%20Activate%20the%20Site%20Features%3CBR%20%2F%3Efunction%20EnableSiteFeature()%7B%3CBR%20%2F%3Etry%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%24site%20%3D%20%24ctx.Web%3CBR%20%2F%3E%23Check%20the%20Feature%20Status%3CBR%20%2F%3E%24FeatureStatus%20%3D%20%24site.Features.GetById(%24siteFeatureId)%3CBR%20%2F%3E%24FeatureStatus.Retrieve(%22DefinitionId%22)%3CBR%20%2F%3E%24ctx.Load(%24FeatureStatus)%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3CBR%20%2F%3E%3CBR%20%2F%3E%23Activate%20the%20feature%20if%20its%20not%20enabled%20already%3CBR%20%2F%3Eif(%24FeatureStatus.DefinitionId%20-eq%20%24null)%20%7B%3CBR%20%2F%3E%23%20Add%20the%20feature%20using%20feature%20id%20and%20scope.%3CBR%20%2F%3E%24newSiteFeature%20%3D%20%24site.Features.Add(%24siteFeatureId%2C%20%24true%2C%20%5BMicrosoft.SharePoint.Client.FeatureDefinitionScope%5D%3A%3ANone)%3CBR%20%2F%3E%24newSiteFeature.Retrieve(%22DisplayName%22)%3CBR%20%2F%3E%24ctx.Load(%24newSiteFeature)%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3CBR%20%2F%3Eif(%24newSiteFeature.DefinitionId%20-ne%20%24null)%7B%3CBR%20%2F%3EWrite-Host%20%24newSiteFeature.DisplayName%20%22Site%20Feature%20activated%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%7B%3CBR%20%2F%3EWrite-Host%20%22Error%20in%20activating%20site%20feature%20-%20%24siteURL%20%3A%20%24(%24_.Exception.Message)%22%20-ForegroundColor%20Red%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20%26amp%3B%20Regards%2C%3C%2FP%3E%3CP%3EBhanu%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-204865%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Frequent Contributor

Hello Everyone,

 

I have come across a strange issue when using the PowerShell Script using CSOM for activating Publishing feature in SharePoint Online site collection. When i activated using the below script - I see that the Pages & Images library are not getting created. Can anyone help in finding out the issue here.

 

=====================

 

Input CSV passed with feature ids -

Sitef6924d36-2fa8-4f0b-b16d-06b7250180fa
Web94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb

 

================================== 

# Activate the Site Collection Feature
function EnableSiteCollectionFeature(){
try{
$site = $ctx.Site
#Check the Feature Status
$FeatureStatus = $site.Features.GetById($siteFeatureId)
$FeatureStatus.Retrieve("DefinitionId")
$ctx.Load($FeatureStatus)
$ctx.ExecuteQuery()

#Activate the feature if its not enabled already
if($FeatureStatus.DefinitionId -eq $null) {
# Add the feature using feature id and scope.
$newSiteFeature = $site.Features.Add($siteFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)
$newSiteFeature.Retrieve("DisplayName")
$ctx.Load($newSiteFeature)
$ctx.ExecuteQuery()
if($newSiteFeature.DefinitionId -ne $null){
Write-Host $newSiteFeature.DisplayName "Site Collection Feature activated"
}
}
}
catch{
Write-Host "Error in activating site Collection feature - $siteURL : $($_.Exception.Message)" -ForegroundColor Red
}
}

# Activate the Site Features
function EnableSiteFeature(){
try{

$site = $ctx.Web
#Check the Feature Status
$FeatureStatus = $site.Features.GetById($siteFeatureId)
$FeatureStatus.Retrieve("DefinitionId")
$ctx.Load($FeatureStatus)
$ctx.ExecuteQuery()

#Activate the feature if its not enabled already
if($FeatureStatus.DefinitionId -eq $null) {
# Add the feature using feature id and scope.
$newSiteFeature = $site.Features.Add($siteFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)
$newSiteFeature.Retrieve("DisplayName")
$ctx.Load($newSiteFeature)
$ctx.ExecuteQuery()
if($newSiteFeature.DefinitionId -ne $null){
Write-Host $newSiteFeature.DisplayName "Site Feature activated"
}
}
}
catch{
Write-Host "Error in activating site feature - $siteURL : $($_.Exception.Message)" -ForegroundColor Red
}
}

 

 

Thanks & Regards,

Bhanu

0 Replies