Publishing Features Activation Through Code in SharePoint 2013 CSOM

Introduction

According to MSDN Publishing in SharePoint 

“is the authoring and deploying of branded artifacts, content, custom assemblies, and configuration files across a Microsoft SharePoint Server 2010 farm. Publishing in SharePoint Server 2010 consists of two separate features. The SharePoint Server Publishing Infrastructure feature provides publishing functionality at the site collection level, and the SharePoint Server Publishing feature provides publishing functionality at the site level. The subset of features and functionality of each feature supports the goal of publishing as part of a Web content management solution.”

In this article, we will see how to activate features through Server Side Object Model both at Site and Site Collection Level.

Code

using (SPSite site = new SPSite(SiteUrl))

{

    site.AllowUnsafeUpdates = true;

    //Activate the publishing feature at the site collection level

    SPFeatureCollection sFeatureCollect = site.Features;

    sFeatureCollect.Add(new Guid("F6924D36-2FA8-4f0b-B16D-06B7250180FA"), true);

    site.AllowUnsafeUpdates = false;

}

using (SPSite site = new SPSite(SiteUrl))

{

    using (SPWeb web = site.OpenWeb())

    {

        web.AllowUnsafeUpdates = true;

        //Activate the publishing feature at the web level

        SPFeatureCollection wFeatureCollect = web.Features;

        wFeatureCollect.Add(new Guid("94C94CA6-B32F-4da9-A9E1F3D343D7ECB"),true);

        web.AllowUnsafeUpdates = false;

    }

}

Code Walkthrough

SharePoint Server Publishing Infrastructure is the feature to be activated at the site Collection Level(web). The first five line of code corresponds to that. Feature ID for the SharePoint Server Publishing Infrastructure is ("F6924D36-2FA8-4f0b-B16D-06B7250180FA").And the parameter true is passed to force the activation. 

AllowUnsafeUpdates is set to true when you are trying to update the database as a result of the GET request. And then set immediately

AllowUnsafeUpdates = false protects from cross site scripting.

SharePoint Server Publishing is the feature to be activated at the site Level. The first five line of code corresponds to that. Feature ID for the SharePoint Server Publishing Infrastructure is ("94C94CA6-B32F-4da9-A9E1F3D343D7ECB ").And the parameter true is passed to force the activation.

Up Next
    Ebook Download
    View all
    Learn
    View all