I'm using the below code to activate the "SharePoint Server Publishing Infrastructure" feature for a site collection. My code runs fine when I'm logged in as a site collection administrator. The purpose is a less privileged user can also activate this feature, through my custom app page with a custom button with the following code behind.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite CurrentSite = SPContext.Current.Site;
CurrentSite.AllowUnsafeUpdates = true;
CurrentSite.Features.Add(new Guid("{f6924d36-2fa8-4f0b-b16d-06b7250180fa}"));
CurrentSite.AllowUnsafeUpdates = false;
});
}
For a less privileged user the pressing teh button results in the access denied page. And below data in the error logs:
Feature Activation: Activating Feature 'PublishingPrerequisites' (ID: 'a392da98-270b-4e85-9769-04c0fde267aa') at URL
Feature Activation: Failed to ensure feature dependencies for feature 'PublishingSite' (id: 'f6924d36-2fa8-4f0b-b16d-06b7250180fa'), exception thrown: System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()
at Microsoft.SharePoint.Utilities.SPUtility.Redirect(String url, SPRedirectFlags flags, HttpContext context, String queryString)
at Microsoft.SharePoint.Utilities.SPUtility.RedirectToAccessDeniedPage(HttpContext context)
at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(HttpContext context)
at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex)
at Microsoft.SharePoint.SPSecurableObject.CheckPermissions(SPBasePermissions permissionMask)
at Microsoft.SharePoint.SPSecurity.ValidateSecurityOnOperation(SPOperationCode code, SPSecurableObject obj)
at Microsoft.SharePoint.SPFeature.AddRowToFeaturesTable(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce)
at Microsoft.SharePoint.SPFeatureCollection.AddInternal(SPFeatureDefinition featdef, Version version, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly)
at Microsoft.SharePoint.SPFeatureCollection.CheckSameScopeDependency(SPFeatureDefinition featdefDependant, SPFeatureDependency featdep, SPFeatureDefinition featdefDependency, Boolean fActivateHidden, Boolean fUpgrade, Boolean fForce, Boolean fMarkOnly)
at Microsoft.SharePoint.SPFeatureCollection.CheckFeatureDependency(SPFeatureDefinition featdefDependant, SPFeatureDependency featdep, Boolean fActivateHidden, Boolean fUpgrade, Boolean fForce, Boolean fMarkOnly, FailureReason& errType)
at Microsoft.SharePoint.SPFeatureCollection.CheckFeatureDependencies(SPFeatureDefinition featdef, Boolean fActivateHidden, Boolean fUpgrade, Boolean fForce, Boolean fThrowError, Boolean fMarkOnly, List`1& missingFeatures)
at Microsoft.SharePoint.SPFeatureCollection.CheckFeatureDependencies(SPFeatureDefinition featdef, Boolean fActivateHidden, Boolean fUpgrade, Boolean fForce, Boolean fMarkOnly)
at Microsoft.SharePoint.SPFeatureCollection.AddInternal(SPFeatureDefinition featdef, Version version, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly)
Updated (Working!) Code:
private void SubmitButton_Click(object sender, EventArgs e){
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite CurrentSite = new SPSite(SPContext.Current.Site.ID))
{
Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest();
CurrentSite.Features.Add(new Guid("{f6924d36-2fa8-4f0b-b16d-06b7250180fa}"));
});
}