Introduction:


Here we will discuss how we can activate SharePoint Server Publishing feature using .Net managed object model code in SharePoint 2016. This also works for SharePoint 2013.

If you are new to .Net managed object model code, we have to add the below two dlls.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll

Here in this example we will create a windows application using Visual Studio 2015 and in the windows application first add the above dlls.

Activate SharePoint Server Publishing feature using .Net managed object model:


In this example let us add the below in the button click.

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:




How to get GUID for features in SharePoint using PowerShell?


In the above code we have used the GUID. To get the GUID for a feature we can use PowerShell. Below PowerShell command can be used.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

Get-SPFeature -Limit ALL | Where-Object {$_.DisplayName -eq "PublishingWeb"}




DeActivate SharePoint Server Publishing feature using .Net managed object model:


In this example let us add the below in the button click.

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.


References:


Also if you want to learn new Microsoft Flow then you can read below articles.