How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell

In this SharePoint csom tutorial, we will discuss how to activate and deactivate publishing feature programmatically using the client-side object model (csom) in SharePoint Online and SharePoint 2013/2016. Also, we will see, how to activate SharePoint Publishing Feature Programmatically using PowerShell.

We may need to activate publishing feature programmatically in SharePoint.

[toc]

An introduction to SharePoint server publishing feature

Before discussing how to activate the SharePoint server publishing feature, let us have an introduction to SharePoint server publishing feature.

A question always comes like what will happen if I will activate the publishing feature in my SharePoint site collection.

If you have a team site and you are trying to activate the publishing feature then you will notice that some document libraries have been created recently (With new! icon will be there with them). If you have created your site collection by using Publishing template or enterprise wiki site collection template then by default all the publishing features will be available.

Basically, there are two features need to be activated for publishing feature:

  • SharePoint Server Publishing Infrastructure (Site Collection Level)
  • SharePoint Server Publishing (Site Level)

The 1st feature will provide publishing functionality at the site collection level and the 2nd one will provide functionality at the site level.

Below are a few changes happened when you activate the publishing feature in the site collection or the particular site.

Activate the SharePoint Server Publishing Infrastructure feature (Site Collection Level)

Once you activate the SharePoint Server Publishing Infrastructure feature it will add the below site templates, so that user can use these templates to create sites also.

  • Publishing Site
  • Publishing Site with Workflow
  • Enterprise Wiki

Below are the groups added to the site collection.

  • Approvers
  • Designers
  • Hierarchy Managers
  • Quick Deploy Users
  • Restricted Readers
  • Style Resource Readers

Also, it will add below permission levels at the site collection level.

  • Approve
  • Manage Hierarchy
  • Restricted Read

It will add below links under Site Administration at the site level as well as the site collection level.

  • Content and structure
  • Content and structure logs

Under the Look and Feel section it adds Navigation, Master Page, Page Layouts and site templates, Welcome page links.

Under Site Collection Administration it adds below links:

  • Site collection navigation
  • Variations Settings
  • Variation labels
  • Variation logs
  • Suggested Content Browser Locations
  • Site collection cache profiles
  • Site collection object cache
  • Site collection output cache

New page layouts like article pages and a Wiki page will be added.

Few items like stylesheet or images will be added to the Style Library.

Few list and document libraries will be added into the site collection like:

  • Images
  • Reusable Content
  • Site Collection Images
  • Content and Structure Reports
  • Pages
  • Site Assets
  • Documents
  • Site Collection Documents
  • Workflow Tasks

Below web parts added to the site collection:

  • Content Query Web Part
  • Media Web Part
  • Summary Links Web Part
  • Table Of Contents Web Part

Few editing features come to the page ribbon-like to add text, images, and rich media to a page. Also, the option to approve a pending version of the publishing page.

Master page will have to publish button will also come. So that user needs to publish a page if they modified something. There are also additional options added to the Page tab.

To activate the publishing feature we need to activate below two features.

  • SharePoint Server Publishing Infrastructure (Site collection level feature)
  • SharePoint Server Publishing (Site Level)

We will write the code for both SharePoint on-premise as well as SharePoint Online.

To activate the feature we need to pass the GUID. Below is the Guid for the corresponding feature.

  • SharePoint Server Publishing Infrastructure (f6924d36-2fa8-4f0b-b16d-06b7250180fa)
  • SharePoint Server Publishing (94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb)

You can check out this msdn article for know more about the feature id for various out of box features in SharePoint 2013.

Activate publishing feature in SharePoint

We can also manually activate publishing feature in SharePoint 2013/2016/2019.

If you have created any site which is not a publishing site, then you can activate the publishing feature in the team sites or any other sites.

Here we will see different ways to activate publishing feature in SharePoint 2013/2016.

  • Activate publishing feature manually using Browser
  • Activate publishing feature using PowerShell

Activate publishing feature manually using Browser in SharePoint 2013/2016/2019

We can easily activate SharePoint publishing feature manually using browser in SharePoint 2013/2016.

Navigate to SharePoint Site Settings >> Click on “Site collection features” link under “Site collection administration“. Click on “Activate” button next to “SharePoint Server Publishing Infrastructure

activate publishing feature sharepoint 2013
activate publishing feature sharepoint 2013

Activate publishing feature SharePoint 2013/2016 PowerShell

Now, we will see how we can activate publishing feature in SharePoint 2013/2016 using PowerShell.

Below is the PowerShell script which will activate the publishing feature in a site collection and all its sub-sites in SharePoint 2013/2016/2019.

You can write, debug, test the PowerShell script using visual studio code or using windows PowerShell ISE.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$WebAppURL="http://hqRajdev13/"
$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
        }
    }
}
Activate publishing feature SharePoint 2013 PowerShell
Activate publishing feature SharePoint 2013 PowerShell

Note: You need to update your web Application URL instead of the above-given URL. The few of the line is showing an error because I given the TEST URL. Once you update the correct URL, then the code will execute fine as I tested this code properly in my Machine.

Get GUID of SharePoint server publishing features using PowerShell

We can also retrieve the GUID of SharePoint server publishing features in SharePoint using PowerShell.

Below PowerShell command can be used in Windows PowerShell ISE.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Get-SPFeature -Limit ALL | Where-Object {$_.DisplayName -eq "PublishingWeb"}
activate publishing feature sharepoint 2013 programmatically
enable sharepoint server publishing feature

If you are new to .Net managed object model code, we have to add the below two dlls to work with CSOM/

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

Here in this example, we will create a windows application using visual studio and in the windows application first, add the above dlls.

Activate SharePoint Server Publishing feature using CSOM

We will see now, how to activate the SharePoint server publishing feature using CSOM in SharePoint server 2013/2016/2019.

using (var ctx = new ClientContext("http://mypc:29024/sites/SPTraining/SP2016Training/"))
{
var features = ctx.Web.Features;
ctx.Load(features);
ctx.ExecuteQuery();
var featureId = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
features.Add(featureId, true, FeatureDefinitionScope.None);
ctx.ExecuteQuery();
label1.Text = " Operation Completed";
}

Once you will run the code the “SharePoint Server Publishing feature” will get activated below:

activate sharepoint server publishing feature using powershell
activate sharepoint server publishing feature using powershell

Deactivate SharePoint Server Publishing feature using csom

Now, we will see how to deactivate the SharePoint server publishing feature using CSOM in SharePoint server 2013/2016/2019.

using (var ctx = new ClientContext("http://mypc:29024/sites/SPTraining/SP2016Training/"))
{
var features = ctx.Web.Features;
ctx.Load(features);
ctx.ExecuteQuery();
var featureId = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
features.Remove(featureId, true);
ctx.ExecuteQuery();
label1.Text = "Operation Completed";
}

Once you will run the code the “SharePoint Server Publishing feature” will get deactivated.

Activate SharePoint Server Publishing feature using CSOM in SharePoint Online

Now, we will see how to activate the SharePoint server publishing feature using CSOM in SharePoint Online.

public static bool ActivateFeature(string siteURL, bool forceActivationFlag)
{
Guid SiteFeatureID = Guid.Parse("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
Guid WebFeatureID = Guid.Parse("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
var output = true;
try
{
using (ClientContext context = new ClientContext(siteURL))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
var site = context.Site;
context.Load(site);
var Web = context.Web;
context.Load(Web);
var siteFeatures = context.Site.Features;
var webFeatures = context.Web.Features;
context.Load(siteFeatures);
context.Load(webFeatures);
context.ExecuteQuery();
siteFeatures.Add(SiteFeatureID, forceActivationFlag, FeatureDefinitionScope.None);
webFeatures.Add(WebFeatureID, forceActivationFlag, FeatureDefinitionScope.None);
context.ExecuteQuery();
}
}

catch (Exception ex)
{
output = false;
}
return output;
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}

Once you run the above code, it will activate both Publishing features in SharePoint Online.

Unable to activate SharePoint Server Publishing site feature in SharePoint

Recently in one of our requirements, we were in need to activate the SharePoint Server Publishing feature at the Site collection level as well as at the site level. We were able to activate the “SharePoint Server Publishing Infrastructure” feature at the site collection level, but when we tried to activate the “SharePoint Server Publishing” at the site level, we got the error which says:

Sorry, something went wrong An unexpected error has occurred. Technical Details

Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID:{our correlation id}

Then we found out that we need to activate the feature using PowerShell command or SharePoint Management Shell with force. The command is below:

$siteUrl = "http://SiteURL"
$siteCollection = Get-SPSite $siteUrl
Enable-SPFeature "PublishingSite" -Url $siteCollection.Url -force
Enable-SPFeature "PublishingWeb" -Url $siteCollection.Url -force

This way we can resolve Sorry, something went wrong An unexpected error has occurred error while activating SharePoint server publishing feature.

How to activate SharePoint Publishing Feature Programmatically using PowerShell

Let us discuss how to enable or disable SharePoint 2013 publishing feature using PowerShell in SharePoint 2013/2016. Learn how to activate publishing feature SharePoint 2013/2016 using PowerShell. We will also see how to enable the publishing feature in SharePoint 2010.

Here we will first disable the publishing feature and then we will enable the publishing feature in SharePoint.

Enable or Disable publishing features SharePoint 2013 PowerShell

Now, we will discuss how to enable or disable publishing features in SharePoint 2013/2016 using PowerShell.

Add-PSSnapin “Microsoft.SharePoint.PowerShell”

Get-SPWeb -site http://SiteCollectionURL -limit ALL | ForEach-Object {Disable-SPFeature Publishing -Url $_.Url -confirm:$false}

Get-SPWeb -site http://SiteCollectionURL -limit ALL | ForEach-Object {Disable-SPFeature PublishingWeb -Url $_.Url -confirm: $false}

Get-SPWeb -site http://SiteCollectionURL -limit ALL | ForEach-Object {Enable-SPFeature Publishing -Url $_.Url -confirm:$false}

Get-SPWeb -site http://SiteCollectionURL -limit ALL | ForEach-Object {Enable-SPFeature PublishingWeb -Url $_.Url -confirm: $false}

Activate SharePoint 2013 Server Publishing Feature using PowerShell

This section we will discuss, how to enable the SharePoint 2013 server publishing feature using PowerShell. If within a Site collection if you have multiple subsites, then PowerShell will be very much helpful in activating the Publishing feature in all subsites, we do not need to go to each site and activate from the browser.

Enable Publishing feature in SharePoint using PowerShell (Site Collection Level)

Below is the PowerShell command to activate publishing feature in all sites and subsites within a site collection.

$site = Get-SPSite https://SiteCollection URL/
$site | Get-SPWeb -limit all | ForEach-Object {Enable-SPFeature -Identity “PublishingWeb” -Url $_.Url}
$site.Dispose()

Enable Publishing feature in SharePoint using PowerShell (Individual Site Level)

Below is the PowerShell command if you want to activate the publishing feature for a particular site.
Individual Site:

Enable-SPFeature -Identity PublishingWeb -URL https://SiteURL/ -Force

SharePoint 2013/2016: PowerShell check if feature is activated

Now, we will see how to check if a feature is activated in SharePoint 2013/2016/2019 using PowerShell. By using PowerShell, we can get to know about feature activation status in SharePoint 2013/2016/2019.

Every SharePoint feature has a feature Id and here we will use the feature id to know the activation status. SharePoint 2013/2016: PowerShell check if feature is activated

Below is the PowerShell command to check if a feature is activated or not.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$featureGuid = "22a9ef51-737b-4ff2-9346-694633fe4416"
$web = Get-SPWeb https://SiteURL.com/marketing/web/
$feature = $web.Features[$featureGuid]
if ($feature -eq $null) { "does not activated" } else { "activated" }

It will display “activated” if the feature status is activated and it will display “does not activated” if the SharePoint feature is not activated. The code will work in SharePoint 2013 and SharePoint 2016.

check if a feature is activated in SharePoint using PowerShell
PowerShell check if feature is activated

Here, we learned how to check a feature is activated using PowerShell in SharePoint 2013/2016.

Error while activating SharePoint Server Publishing Feature in SharePoint 2013/2016

Now we will discuss, how to resolve the access denied issue when the user tried to activate or deactivate the SharePoint server publishing feature. The user who was trying to activate was having full control permission is site collection as well as a subsite.

The user was having full control in the subsite as well as in the site collection, but still when the user was trying she was getting access denied.

The user should be a site collection administrator to activate or deactivate the feature on the subsite. Or the user should be a part of “Hierarchy Managers” group for the site collection that contains the subsite.

Then only the user will be activated or deactivate the feature in SharePoint 2013/2016.

The feature failed to activate because a list at PublishingImages already exists in this site

Recently in a migration project, we faced one issue while trying to activate SharePoint 2013 publishing feature in a subsite. The error comes as:

Sorry, something went wrong
The feature failed to activate because a list at ‘PublishingImages’ already exists in this site. Delete or rename the list and try activating the feature again.
TECHNICAL DETAILS

Troubleshoot issues with Microsoft SharePoint Foundation.
Correlation ID: dd9a469d-9e16-a03f-d3a2-794a2716f9d9

The issue basically comes for us in MOSS 2007 to SharePoint 2013 migrated sites. I got two approaches to solve the issue.

First Approach:
We need to go to the PublishingImages library and rename the document library name to some other from the library settings page.

Then try to activate the publishing feature.

Second Approach:
For few subsites the other approach I have tried is I have activated the Publishing feature by using PowerShell command by using the -Force parameter.

The PowerShell command is like below:

Enable-SPFeature -Identity PublishingWeb -URL https://SiteURL/ -Force

Enable publishing features in SharePoint 2010

We will also see now, how to enable publishing features in SharePoint 2010 sites also,

Before creating any publishing site in your site collection you need to enable publishing feature. If you will not enable then you will get the below error: The Office SharePoint Server Publishing Infrastructure feature must be activated at the site collection level before the Publishing feature can be activated.

So to enable publishing feature to follow below step:

Go to Site Actions -> Site Settings as shown in the figure below:

Enable publishing features in SharePoint 2010

This will open the Site Settings page. From the Site Settings page go to the Site Actions section and click on Manage Site Features as shown in the figure below.

Enable publishing features in SharePoint 2010

This will open the list of features and select SharePoint Server Publishing and click on Activate as shown in the figure below.

Steps to Enable publishing features in SharePoint 2010

You may like following SharePoint tutorials:

This SharePoint tutorial explains, how to activate and deactivate SharePoint server publishing features in SharePoint Online as well as in SharePoint Online.

>