Activate and Deactivate SharePoint Server Publishing feature using .Net managed object model in SharePoint 2016

Get guid using powershell sharepoint 2013
InfoPath alternatives for form designing SharePoint

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.

Also you can check below articles:
– Working with Field Template using JSLink in SharePoint 2013

– SharePoint Online Add Update Delete Item from List using SPServices

– Add Menu to ECM List Item Menu in SharePoint 2013 using SharePoint Designer 2013

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:

Activate SharePoint Server Publishing feature using client object model
Activate SharePoint Server Publishing feature using client object model

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”}

Get guid using powershell sharepoint 2013
Get guid using powershell sharepoint 2013

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.

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About 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

View all posts by Bijay Kumar →

Leave a Reply