Activate SharePoint Server Publishing Feature programmatically using C#.Net .net managed object model code

Here we will discuss how we can activate the SharePoint Server Publishing Feature programmatically using C#.Net .net client managed object model code (CSOM) in SharePoint 2013. Due to one of the business requirement we need to activate the publishing feature on our team site. To activate the publishing feature we need to activate below two features. The same csom code we can use to activate the publishing feature in SharePoint 2016 or SharePoint 2010 also. SharePoint online (Download PDF for FREE)

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

SharePoint deveopment training course

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.



Below is the full code:

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


Read some sharepoint online tutorials:

Once you run the above code, it will activate both Publishing features in SharePoint 2013. Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →