Navigate Up
Sign In
Supporters of Developer
Web

SharePoint Feature Stapling: Create the Feature

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

You may also be interested in: Build a Scalable Architecture by KnowledgeLake

 

Editor's note: Contributor Chris Grist is a Senior Technical Consultant for Loftus IT. Follow him @gristdog

In this two part series, you will learn how simple feature stapling can be. The first article will actually build a useful site collection feature that if your outside the US you will find very handy.

So what is feature stapling?

Quite simply feature stapling allows you to instruct SharePoint to activate features (that have been stapled), when a web is created. This can ensure certain features are always activated for particular site templates or for anything. This can be a useful way to hide utility features like the one we are going to build today away from users who have the rights to activate/deactivate features. I also use it quite commonly to apply branding to site collections created by non-SharePoint administrators such as My Sites.

Let’s make a feature

The first thing we are going to do is make a feature to actually staple, this way we can see the process from end to end and get a useful feature at the end of it as well.

The feature that we are going to build is a nice utility feature that is going to accomplish the following:

  1. Set the Locale of the site collection to Australia.
  2. Set the Search Centre to be /Search/Pages
  3. Set the Scopes Dropdown to be “Show scopes dropdown”

Pretty simple stuff, but getting this automatic and consistent will help with governance in the SharePoint farm and also save SharePoint Admins some time. I normally also set branding to be activated and any other farm specific features, but the above should be applicable for most people.

Step 1: Create the Solution

First thing you are going to do is fire up Visual Studio 2010 and create a New Project. Create a SharePoint Empty Project with a meaningful name.

2012-06-06-FeatureStapling-Part01-01.png

I am going to deploy this as a farm solution to one of my test sites. Once you have completed the wizard, you will end up with something like this:

2012-06-06-FeatureStapling-Part01-02.png

Step 2: Add the Feature

As we are not going to be adding any components to this solution, we need to first add the feature manually. Right click on the features folder, and select Add Feature.

2012-06-06-FeatureStapling-Part01-03.png

This will open the new feature Feature1.feature up in the new window. Configure a decent name and description, and set the scope to Site which is the site collection level.

2012-06-06-FeatureStapling-Part01-04.png

Whilst still on this screen at the bottom of the pane click on “Manifest”, this will generate a preview of the packaged manifest, scroll across until you find “Id=”. This is the Feature ID and we will use this in Part 2, so keep it safe for now and it will save time later.

2012-06-06-FeatureStapling-Part01-05.png

I would also recommend you select the feature in the solution explorer and rename it to something meaningful. I often suffix the scope of the feature to make it simple to see which scope the feature is going to be installed at.

2012-06-06-FeatureStapling-Part01-06.png

Now expand the plus symbol next to the feature, until you reveal the feature template xml. Double click this file and it will open in the left pane, to make this feature hidden you will need to configure the Hidden parameter as shown below, this is case sensitive so I recommend using intelisense when modifying the template file.

2012-06-06-FeatureStapling-Part01-07.png

Step 3: Add the Feature Receiver

In the previous step we did all the ground work to configure our feature, something that you would do with pretty much every feature you create for SharePoint. The next step is to add the feature receiver, our functionality is so simple that we can do this all from within a feature activated receiver.

To add the feature receiver, simply right click the feature and select Add Event Receiver.

2012-06-06-FeatureStapling-Part01-08.png

This will generate a class file, it has several commented out methods that activate when different actions occur to our feature, right now we are only interested in the FeatureActivated one, so uncomment this.

2012-06-06-FeatureStapling-Part01-09.png

Step 4: Add the Code!

We are now ready to add the code in to our feature receiver.

The first step is to get the Site Collection this feature is running on, and then the root web for that site collection. This can be accessed via the SPFeatureReceiverProperties as shown below.

2012-06-06-FeatureStapling-Part01-10.png

The code we are going to add is going to go where I have left the comment. The using statements make sure any objects used are disposed when we are done.

As a recap we were looking to achieve three outcomes with this feature:

  1. Set the Locale of the site collection to Australia.
  2. Set the Search Centre to be /Search/Pages
  3. Set the Scopes Dropdown to be “Show scopes dropdown”

This is how we can achieve them:

  1. Set the Locale of the site collection to Australia.

  2. topLevelSite.Locale = new System.Globalization.CultureInfo(3081);
    

    This piece of code simply sets the Locale of the web to a LCID of 3081 which is Australia. You can find a longer list of locales for your region here: http://msdn.microsoft.com/en-us/goglobal/bb895996

  3. Set the Search Centre to be /Search/Pages

  4. topLevelSite.AllProperties["SRCH_ENH_FTR_URL"] = “/Search/Pages”;
    

    This value is simply a string stored in the property bag for the web.

  5. Set the Scopes Dropdown to be “Show scopes dropdown”

  6. topLevelSite.AllProperties["SRCH_SITE_DROPDOWN_MODE"] = “ShowDD”;
    

    The value for the scopes drop down is also stored in the property bag as a string, but there are a specific set, depending on what you want the value to be. If you take a look at the drop down you get the following values:

    2012-06-06-FeatureStapling-Part01-11.png

    The following values correspond with the drop down selection:

  • HideScopeDD – Do not show scopes dropdown, and default to contextual scope.
  • HideDD_NoScope - Do not show scopes dropdown, and default to target results page.
  • ShowDD – Show scopes dropdown.
  • ShowDD_DefaultURL – Show, and default to ‘s’ URL parameter.
  • ShowDD_DefaultContextual – Show and default to contextual scope.
  • ShowDD_NoContextual – Show, do not include contextual scopes.
  • ShowDD_NoContextual_DefaultURL – Show, do not include contextual scopes, and default to ‘s’ URL parameter

After this our entire block of code will look like the following, the extra code you will need is to execute the update statement:

2012-06-06-FeatureStapling-Part01-12.png

You probably won’t write many feature receivers as simple as that, basically four lines of code plus setup, to achieve the three goals we had at the start. If you want to make sure your feature is having the desired affect before we continue, simply remove the hidden attribute, deploy to an existing site collection and activate the feature to see if it has the desired effect.

Note: I had some issues when updating the feature receiver, the web application was caching the old code, to get around this i simply ran an iisreset and it picked up the new code and ran as expected.

Categories: dev; MOSS; WSS; 2010

Comments

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: