Print Shortlink

Activating SharePoint 2010 features with properties

The most common task when deploying your solution is to activate custom and out of the box features. In the first chapter of my book – we looked at how you can automate the process using PowerShell saving yourself from manual deployment of your solution. However, PowerShell snap in for SharePoint doesn’t have a method to activate features with properties. There are quite a few of out of the box features with properties, let alone your might have few of your own if you want to pass parameters to your features.

Recently I found very good reference over here on how you can use .NET reflection and pass activation properties to your features. In this article I would like to provide simplified example that you can compile quickly and possibly latest show you how same can be done in PowerShell.

First, we’re going to create a new SharePoint solution which will provision just one feature with receiver.

1. In your Vsiaul Studio Blank SharePoint project add a feature
2. Add a receiver to the feature
3. Add the following code to the receiver’s .Activated method:

public override void .Activated(SP.ReceiverProperties properties)
{
SPWeb web = properties..Parent as SPWeb;
if (properties..Properties["Test"]!=null)
{
web.Title = properties..Properties["Test"].Value;
web.Update();
}
}

Basically a new feature parameter called “Test” is being accepted and the value of that parameter will be set as a title of the web. Deploy the solution – you will notice the feature will be installed in your site but not activated.

Let’s see how we can activate the feature with parameters using a custom webpart

1. Create a new SharePoint project in Visual Studio of Visual Webpart 2. In the code behind of of the user control add the following code to the Page_Load method:

 protected void Page_Load(object sender, EventArgs e)
{
    SPWeb web = SPContext.Current.Web;
    web.AllowUnsafeUpdates = true;
    Dictionary<string,string> activationProps = new Dictionary<string,string>();
    activationProps.Add("Test", "This is my title");
    web..s.Activate.(new Guid("1a3fea9d-251d-42ce-a72e-5f138629c0c1"),
activationProps);
    web.Update();
}

Above, the GUID is simply the ID of the feature we created before. In this part we’re passing a parameter to our extension method called Activate. with the properties and the ID of the feature. Let’s now implement the extension method provided by Hristo in the above article:

The extension class (with 2 methods) will go into the separate class and will be referenced by the user control, here is the code of the class:

public static class ExtensionFor.
{
public static SP. Activate.(this
SP.Collection features,
Guid featureId,
Dictionary<string,string> activationProps)
{
ConstructorInfo propCollConstr =
typeof(SP.PropertyCollection).GetConstructors(BindingFlags.NonPublic
| BindingFlags.Instance)[0];
SP.PropertyCollection properties = (SP.PropertyCollection)
propCollConstr.Invoke(new object[] { null });
foreach (string key in activationProps.Keys)
properties.Add(new SP.Property(key, activationProps[key]));
return Activate.(features, featureId, properties);
}
private static SP. Activate.(this
SP.Collection features,
Guid featureId,
SP.PropertyCollection properties)
{
MethodInfo get.Internal =
typeof(SP.Collection).GetMethod(
"Get.",
BindingFlags.Instance generic ambien BindingFlags.NonPublic,
null,
new Type[] { typeof(Guid) },
null);
SP. alreadyActivated. = (SP.)get.Internal.Invoke
(features, new object[] { featureId });
if (alreadyActivated. != null)
// The feature is already activated. No action required
return null;
MethodInfo addInternal =
typeof(SP.Collection).GetMethod(
"Add",
BindingFlags.Instance generic ambien BindingFlags.NonPublic,
null,
new Type[] { typeof(Guid), typeof(SP.PropertyCollection), typeof(bool) },
null);
object result = addInternal.Invoke(features, new object[] { featureId,
properties, false });
return result as SP.;
}
}

Add the following namespace references:

using System.Reflection;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

The above extension class will use Reflection to access internal methods available in SharePoint and invoke those to pass parameters into your feature during activation.

Now, deploy your solution and add a new webpart to the page – during its activation you will see the second feature being activated and title of the web changed according to the feature parameter passed.

Enjoy!