Wesley Bakker

Interesting things I encounter doing my job...

Sponsors

News

Wesley Bakker
motion10
Rivium Quadrant 151
2909 LC Capelle aan den IJssel
Region of Rotterdam
The Netherlands
Phone: +31 10 2351035

(feel free to chat with me)
 

Add to Technorati Favorites

SharePoint Central Administration Feature

SharePoint Central Administration Feature

While developing a motion10 feature that enables the modification of the DaysToShowNewIndicator web application setting, I was looking for a way to make sure the feature get’s activated on SharePoint’s Central Administration Web Application only. I decided to apply no less than three techniques for this.

DaysToShowNewIndicatorSettings

AutoActivateInCentralAdmin and Hidden

First of all I removed the need to activate the feature at all. By setting the AutoActivateInCentralAdmin feature element tag to true the feature get’s activated on the Central Administration Web Application on installation. This removes the need to activate the feature for a SharePoint administrator and thus the chance he/she will try to activate this feature on a different web application. Another attribute I have set is the Hidden attribute. I’ve set its value to TRUE. This is to make sure that you can’t activate the feature in the UI.

Here’s the feature.xml:

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="7c631062-2e9c-4252-ab28-b2a3afa7157f"
          Title="motion10 Days To Show New Indicator"
          Description="This feature allows for setting the ammount of days the New indicator shows up next to an item. This feature can only be enabled on a Central Admin site."
          Version="12.0.0.0"
          Hidden="TRUE"
          Scope="WebApplication"
          DefaultResourceFile="core"
          ReceiverAssembly="SharePointSolutionPack, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4a7cd02bdf107f7a"
          ReceiverClass="Motion10.SharePoint2007.CentralAdminOnlyFeatureReceiver"
          AutoActivateInCentralAdmin="TRUE"
          Creator="Wesley Bakker"
          ImageUrl="motion10/FeaturesIcon.png"
          ImageUrlAltText="http://www.motion10.com"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>    
  </ElementManifests>
</Feature>

And once installed you’ll find the Custom Action on your Application Management form:

DaysToShowNewIndicator

But the ‘motion10 Days To Show New Indicator’ feature is not displayed on the ‘Manage Web Application Features’ form:

WebApplicationFeatures

SPFeatureReceiver

The last and final action to take if you do not want anybody to activate your feature on any web application besides the Central Administration is to create a class that implements SPFeatureReceiver. In that class you can implement a check when the feature get’s activated. We’ve already mentioned that class in the feature.xml file above. It’s in the ‘ReceiverAssembly’ and ‘ReceiverClass’ attribute.

The class is very simple and looks like this:

//-----------------------------------------------------------------------
// <copyright file="CentralAdminOnlyFeatureReceiver.cs" company="motion10">
//     Copyright (c) motion10. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
 
using System;
using System.Globalization;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
 
namespace Motion10.SharePoint2007 {
    /// <summary>
    /// You can use the CentralAdminOnlyFeatureReceiver class for those feature that can only be activated on the Central Administration Web Application. On other web applications the feature will be removed and an error is thrown to indicate that activation did not succeed.
    /// </summary>
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
    public class CentralAdminOnlyFeatureReceiver : SPFeatureReceiver {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties) {
            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
            if (!webApp.IsAdministrationWebApplication) {
                Guid featureId = properties.Feature.DefinitionId;
                webApp.Features.Remove(featureId, true);
                webApp.Update();
 
                throw new SPException(string.Format(CultureInfo.InvariantCulture, "You can activate the feature with ID {0} on the Central Administration Web Application only!", featureId));
            }
        }
 
        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            //throw new NotImplementedException();
        }
 
        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
            //throw new NotImplementedException();
        }
 
        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
            //throw new NotImplementedException();
        }
    }
}

On activation of this feature on a SPWebApplication that is not a Central Administration Web Application the feature gets removed immediately and an error is thrown.

Conclusion

With a little bit of effort it’s not that hard to implement a Central Admin only feature. You can reuse the CentralAdminOnlyFeatureReceiver class on other features just well.

Cheers,

Wes

Posted: Jan 28 2009, 02:09 PM by webbes | with 4 comment(s)
Filed under:

Comments

SharePoint Central Administration Feature - Wesley Bakker said:

Pingback from  SharePoint Central Administration Feature - Wesley Bakker

# January 28, 2009 8:40 AM

All About Sharepoint Handy useful Tips « Aswath’s Blog said:

Pingback from  All About Sharepoint Handy useful Tips  &laquo; Aswath&#8217;s Blog

# March 30, 2009 4:36 AM

bob e said:

Good work on that

# June 8, 2009 11:19 AM

sago david said:

thanks for sharing.

# October 21, 2011 6:36 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)