Sunday, March 27, 2016

How to Activate Publishing Feature using PowerShell in SharePoint

Task: SharePoint Publishing sites used to create Public web sites, intranet portals, etc. We had to activate Publishing feature in SharePoint using PowerShell in scenarios, such as:

  • Activating Publishing feature through web user interface resulted in error!
  • Bulk activating publishing feature for all site collections and sites in a web application.
PowerShell to Activate Publishing Feature in SharePoint:
Publishing sites rely on "SharePoint Server Publishing Infrastructure" feature, which needs to be activated both at site collection and site level. To activate publishing
  • Navigate to Site Settings >> Click on "Site collection features" link under "Site collection administration
  • Click on "Activate" button next to "SharePoint Server Publishing Infrastructure"
    sharepoint 2013 enable publishing infrastructure powershell
SharePoint PowerShell to activate publishing feature at site collection:
Here is the PowerShell to activate publishing feature at site collection.
Enable-SPFeature -Identity "PublishingSite" -url "http://intranet.crescent.com"

Lets add some error handling & variables to it:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL="http://intranet.crescent.com"
$FeatureName = "PublishingSite"

#Check if publishing feature is already activated in the site collection
$Feature = Get-SPFeature -site $siteURL | Where-object {$_.DisplayName -eq $FeatureName}
if($Feature -eq $null)
{    
    #Enable the Publishing feature 
    Enable-SPFeature -Identity $FeatureName -url $SiteURL -Confirm:$False
   
    Write-host "Publishing Feature Activated on $($SiteURL)" -ForegroundColor Green    
}
else
{
    Write-host "Publishing Feature is already Active on $($SiteURL)" -ForegroundColor Red
}

Enable SharePoint server publishing at site level:
We have to activate the Publishing feature at site level also.
  • Go back to Site Settings page >> Click on "Manage Site Features" link under "Site Actions" group
  • Click on Activate button next to "SharePoint Server Publishing"
This enables publishing feature at web level. To activate SharePoint server publishing using PowerShell, use this script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebURL="http://intranet.crescent.com/Sales/"
$FeatureName = "PublishingWeb"

#Check if publishing feature is already activated in the site 
$Feature = Get-SPFeature -Web $WebURL | Where-object {$_.DisplayName -eq $FeatureName}
if($Feature -eq $null)
{    
    #Enable the Publishing feature 
    Enable-SPFeature -Identity $FeatureName -url $WebURL -Confirm:$False
   
    Write-host "Publishing Feature Activated on $($WebURL)" -ForegroundColor Green    
}
else
{
    Write-host "Publishing Feature is already Active on $($WebURL)" -ForegroundColor Red
}

Publishing feature must be enabled at site collection before enabling it to sub-sites!

Activate SharePoint server publishing infrastructure for with PowerShell for all site collections & sites in a web application:
At times, You may have to activate or deactivate SharePoint server publishing infrastructure using PowerShell for all webs in entire site collection or even all site collections in the entire web application. Here is the script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebAppURL="http://intranet.crescent.com/"
$SiteFeatureName = "PublishingSite"
$WebFeatureName = "PublishingWeb"

#Get all site collections of the web app and iterate through
$SiteColl= Get-SPSite -WebApplication $WebAppURL -Limit ALL
Foreach($Site in $SiteColl)
{ 
    write-host "Processing site collection:"$Site.URL -ForegroundColor Yellow 
    #Check if publishing feature is already activated in the site 
    $Feature = Get-SPFeature -Site $Site.URL | Where-object {$_.DisplayName -eq $SiteFeatureName}

    if($Feature -eq $null)
    {    
        #Enable the Publishing feature 
        Enable-SPFeature -Identity $SiteFeatureName -url $Site.URL -Confirm:$False
   
        Write-host "Publishing Feature Activated on $($Site.URL)" -ForegroundColor Green    
    }
    else
    {
        Write-host "Publishing Feature is already Active on $($Site.URL)" -ForegroundColor Red
    }

    #Loop through each web in the site collection
    Foreach($Web in $Site.AllWebs)
    {
        write-host "Processing Web"$Web.URL -ForegroundColor Yellow
        #Check if publishing feature is already activated in the web 
        $Feature = Get-SPFeature -Web $Web.URL | Where-object {$_.DisplayName -eq $WebFeatureName}
        if($Feature -eq $null)
        {    
            #Enable the Publishing feature 
            Enable-SPFeature -Identity $WebFeatureName -url $Web.URL -Confirm:$False
   
            Write-host "Publishing Feature Activated on $($Web.URL)" -ForegroundColor Green    
        }
        else
        {
            Write-host "Publishing Feature is already Active on $($Web.URL)" -ForegroundColor Red
        }
    }
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...