Enabling The Publishing Feature SharePoint Online Office 365 Using CSOM

In SharePoint 2013, we have a new site template called Developer Site Template. It is a site template, which is used extensively for app development and you can only deploy apps for SharePoint to a Developer Site.

I have a scenario, where I need to enable Developer Site Collection feature. For an existing Site Collection, it may be team site or publishing site. There isn't a button in the Site Collection features to allow you to enable this.

If you are using SharePoint 2013 On-Premises, you’d just run Enable-SPFeature, which is given below.

Enable-SPFeature 94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb –url http://gowthamr.sharepoint.com

With SharePoint Online, we can't do this directly, so I have created a simple method to solve this. We can run this script in our Internet Browser console.

We have already seen how to activate the developer site feature, using JSOM. Now, we are goin to use CSOM method.
  • FeatureDefinitionScope Enumeration.
  • public enum FeatureDefinitionScope.
Member name description

None- The feature scope is not specified. The value = 0.

Farm Enumeration values specify that the feature scope has to be at the Farm level. The value = 1.

Site Enumeration values specify that the feature scope has to be at the Site Collection level. The value = 2. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Microsoft.SharePoint.Client;  
  6.    
  7. namespace O365SPAddUsertoRole  
  8. {  
  9.     class Program  
  10.     {  
  11.       
  12.       
  13.      private class Configuration  
  14.         {  
  15.                 public static string ServiceSiteUrl = "https:// https://gowthamr.sharepoint.com";  
  16.                 public static string ServiceUserName = "[email protected]";  
  17.                 public static string ServicePassword = "xxxxxxxxxx";  
  18.         }  
  19.   
  20.                  static ClientContext GetonlineContext()  
  21.                           {  
  22.                                     var securePassword = new SecureString();  
  23.                                     foreach (char c in Configuration.ServicePassword)  
  24.                                     {  
  25.                                         securePassword.AppendChar(c);  
  26.                                     }  
  27.                                     var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);  
  28.                                     var context = new ClientContext(Configuration.ServiceSiteUrl);  
  29.                                     context.Credentials = onlineCredentials;  
  30.                                     return context;  
  31.                           }  
  32.   
  33.   
  34.         static void Main(string[] args)  
  35.         {  
  36.             var context=GetonlineContext();  
  37.             Web web = context.Web;   
  38.             var featureId = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");  
  39.             var features = context.Web.Features;  
  40.             features.Add(featureId, true, FeatureDefinitionScope.None);  
  41.             context.ExecuteQuery();  
  42.                  
  43.             
  44.         }  
  45.     }  
  46. }  
Was my blog helpful?

If so, please let me know at the bottom of this page. If not, let me know what was confusing or missing and I’ll use your feedback to double-check the facts, add info and update this blog.