1

Features in SP allow us to perform a limited set of operations. Fortunately, Microsoft created the feature receiver class to allow us to perform additional operations.

However, in a more industrialized world, I'd like to extend the features functionnalities with my own features "kind", to avoid adding for each project the same c# code (I'm quite fond of declarative way)

A good example would be a feature definition that automatically create groups.

Sample target xml :

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Group xmlns="http://my.custom.schema" name="My group">
        <Add Member="domain\user" />
    </Group> 
</Elements>

The idea is to allow a developer to reuse, declaratively, some useful features : creating groups, adding webpart to existing page, etc.

I'm quite sure, however, that my xml file won't be like this, but I'm open to suggestion.

thx

2 Answers 2

2

To answer the question (Andy gives a great example on how it should be done though):

No, you can't extend the schema of the feature framework (as you describes). You have to do it in custom code, for example using a Feature Receiver.

Except from adding properties to an element manifest, you could create your own "manifests". Just deploy a custom XML (with whatever schema you like, so you get Visual Studio intellisense) and parse it using Feature Receiver.

5
  • this is what I want to avoid : having to add code for each feature. My goal is to allow a developer to simply add an xml file of my own schema without having to add code
    – Steve B
    Commented Aug 30, 2011 at 11:48
  • If you define the schema and whatever you want and create 1(one) generic feature receiver then your all good to go. You could even create an SPI for Visual Studio for this, so your devs only add a new item to the project. Commented Aug 30, 2011 at 12:07
  • this sound approaching my goal but with one limitation... I have to separate OOB features elements, with legitimate custom feature receiver, from features with my own schema, as the requirement is to set the receiver to my generic one. Or did I miss something ? this is not a big issue, but it wont allow me to define a single feature with both OOB functionalities and custom ones.
    – Steve B
    Commented Aug 30, 2011 at 12:26
  • Then you're out of luck. As I said - you cannot extend the schema of the feature framework Commented Aug 30, 2011 at 12:55
  • I think I'll end up by mixing your solution with the feature properties approach... not ideal, but a good start point
    – Steve B
    Commented Aug 30, 2011 at 13:10
0

Yes, you can do that, for simple settings at least. I've written Feature Receivers to run C# code for my Features, when activated, and then used that in different Features with different property settings.

The Features themselves can then contain properties that you read in within your assembly. In Feature.xml you'd have something like:

<Properties>
  <Property Key="MyProp" Value="SomeValue"/>
</Properties>

Then in your feature receiver, something like:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  SPSite site = properties.Feature.Parent as SPSite;
  SPWeb web = site.RootWeb;
  if (web != null)
  {
    SPFeatureProperty someProp= properties.Feature.Definition.Properties["MyProp"];
1
  • I was thinking about using properties... but as it's only a key/value collection, it would be a bit hard to put complex structure
    – Steve B
    Commented Aug 30, 2011 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.