views:

28

answers:

3

If I deploy a solution at farm level, is there a way by which i can prevent the owners of the various site collections from activating the features present in that solution?

A: 

If an error occurs during feature activation, it will not activate the feature and will retract any effects which might have been deployed as part of the Elements Manifest.

So, through crafty usage of this, you can use the FeatureActivated portion of a feature receiver to check who is activating it, and throw an UnauthorizedAccessException with an appropriate error message detailing why the feature cannot be activated. This will show up as the standard SharePoint error page with the message you specify. If you already have a feature receiver on the feature, you need to append this at the start of the FeatureActivated portion, so that any programmatic actions don't occur (unlike elements manifest, these are not retracted on unsuccessful activation).

If you haven't used a Feature Receiver before, you just need two parts to establish it.

  1. In the feature XML of your feature, add the following two attributes to the Feature node.

    ReceiverAssembly=(four-part-assembly-string)
    ReceiverClass=(full namespace.class name of receiver class)
    
  2. Write a receiver class. This inherits from SPFeatureReceiver, and has 4 required overrides in FeatureActivated, FeatureDeactivating, FeatureInstalled, and FeatureUninstalling. You don't have to do anything for the last 3. You'll write your security check in the FeatureActivated method.

ccomet
+1  A: 

A simple way to prevent site collection users from activating a certain feature is to mark it as hidden. These features are then effectively only allowed to be activated by farm administrators through STSADM commands.

To hide a feature update the Hidden attribute of the Feature element to ‘TRUE’ as shown below:

<Feature 
      Id="AD2146D-62DA-4911-DBC1-AE177DE40084"
      Title="Restricted Web Parts" 
      Hidden="TRUE"
      .../>

Alternatively if you are using SharePoint 2010 you can use Feature Packs to solve this problem by targeting a set of features to a particular set of users.

Ari
+1  A: 

Have a look at the Zevenseas feature blocker.

Muhimbi