Export (0) Print
Expand All

How to: Create and Deploy Feature Receivers in Sandboxed Solutions

SharePoint 2010

This topic explains how to create and deploy a Feature receiver in a sandboxed solution in Microsoft SharePoint 2010.

Last modified: April 14, 2011

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

To create a Feature receiver

  1. In Microsoft Visual Studio, start an Empty SharePoint Project. When you are prompted, choose to make it a sandboxed solution.

  2. In Solution Explorer, right-click the Features folder, and then select Add Feature.

  3. Right-click the Feature, and select Add Event Receiver.

  4. Open the .cs or .vb file that was created in the previous step.

  5. Uncomment and override the event handlers in the file as needed using your event handling logic. As a general rule, if you override the FeatureActivated handler, you should also override the FeatureDeactivating handler to reverse what your code did in the FeatureActivated handler. Similarly, if you override the FeatureInstalled handler, you should also override the FeatureUninstalling handler to reverse what your code did in the FeatureInstalled handler.

    Important note Important

    Your code must conform to the restrictions on all code that runs in a sandboxed solution. This is the only difference between a Feature receiver in a farm solution and a Feature receiver in a sandboxed solution. For example, your code cannot access the file system of the farm servers; neither can it access anything outside the site collection in which the code is running.

    For example, the following override of FeatureActivated appends a string to the title of the root website of the containing site collection. And the override of FeatureDeactivating reverses this change.

    
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb website = (SPWeb)properties.Feature.Parent;
        SPSite siteCollection = website.Site;
        siteCollection.RootWeb.Title = siteCollection.RootWeb.Title + " with Ketchup!"; 
        siteCollection.RootWeb.Update();
    }
    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SPWeb website = (SPWeb)properties.Feature.Parent;
        SPSite siteCollection = website.Site;
        siteCollection.RootWeb.Title = siteCollection.RootWeb.Title.Replace(" with Ketchup!", string.Empty);
        siteCollection.RootWeb.Update();
    }
    
    
  6. Build, package, and deploy to your development SharePoint installation. When testing and debugging is completed, make the sandboxed solution package (.wsp file) available to site collection administrators.

A site collection administrator can install a sandboxed solution to the site collection's Solution Gallery. There are two steps to this process. First, the solution package is uploaded to the gallery. Second, it is deployed, which is called "activation" for sandboxed solutions. The second step automatically activates any Features in the package. If any solution validators are registered with the site collection, they also execute at the activation stage. If the solution passes validation and has not been blocked by a farm administrator, it is ready for use on the site collection.

Show:
© 2014 Microsoft