Activate SharePoint Server Publishing Feature programmatically using CSOM

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.

We may need to activate publishing feature programmatically in SharePoint.

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.

Get GUID of SharePoint server publishing features in SharePoint 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

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 2017 and in the windows application first, add the above dlls.

Activate SharePoint Server Publishing feature using CSOM SharePoint Server 2013/2016

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

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

Deactivate SharePoint Server Publishing feature using csom in SharePoint server 2013/2016.

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

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 requirement, we were in need to activate the SharePoint Server Publishing feature in Site collection level as well as in the site level. We were able to activate “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.

You may like following SharePoint CSOM tutorials:

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

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>