Thursday, 8 May 2014

How to enable SharePoint Server Publishing Infrastructure


I am enabling at Site Level
private void EnsureSiteFeatureActivated(Guid objFeatureGuid, SPSite objSite)
{
using (SPSite oSite =new SPSite(objSite.ID))
{
oSite.AllowUnsafeUpdates = true;
SPFeature objFeature = null;
try
{
objFeature = oSite.Feature[objFeatureGuid];
}
catch (Exception)
{
oSite.Features.Add(objFeatureGuid, false);
//throw;
}
oSite.AllowUnsafeUpdates = false;
}
}
If you want enable Web Level, use below code.

private void EnsureWebFeatureActivated(Guid objFeatureGuid, SPSite objSite, SPWeb objWeb)
{
using (SPSite oSite =new SPSite(objSite.ID))
{
using (SPWeb oWeb = oSite.OpenWeb(objWeb.ID))
{
oSite.AllowUnsafeUpdates = true;
oWeb.AllowUnsafeUpdates = true;
SPFeature objFeature = null;
try
{
objFeature = oWeb.Feature[objFeatureGuid];
}
catch (Exception)
{
oWeb.Features.Add(objFeatureGuid, false);
//throw;
}
oSite.AllowUnsafeUpdates = false;
oWeb.AllowUnsafeUpdates = false;
}
}
}

You can get GUIDs from below link.
http://microsoft-techies.blogspot.com/2014/05/sharepoint-features-and-their-guids.html