Activate SharePoint Server Publishing Infrastructure Programmatically (SharePoint 2010)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Audience;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;


namespace ConsoleOperations
{
    class ActivatePublishingFeature
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("http://localhost:5012/"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        Guid sitePublishingGuid = new Guid("F6924D36-2FA8-4f0b-B16D-06B7250180FA"); //Site Publishing Feature
                        //Ensure features Exist, if they do not, they will be added to the site to be activated.
                        EnsureSiteFeatureActivated(sitePublishingGuid, site);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }
        private static void EnsureSiteFeatureActivated(Guid featureGuid, SPSite site)
        {
            site.AllowUnsafeUpdates = true;
            SPFeature feature = null;
            try
            {
                feature = site.Features[featureGuid];
                if (feature == null)
                {
                    site.Features.Add(featureGuid, false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            site.AllowUnsafeUpdates = false;
        }
    }
}

Comments

Popular posts from this blog

javascript to get the all site user in sharepoint 2010 (Getting all Site users using Client Object Model)

Adding Navigation Links To Quick Launch Bar Programatically (SharePoint 2010)

Disable all day event, Recurrence,Workspace Fields in Calendar list