Activating features in a Site Subscription Feature Pack

Whilst developing solutions to use the Subscription and Feature Pack frameworks in SharePoint 2010 (lots of great content about that here: http://www.harbar.net/articles/sp2010mt1.aspx) I came across an interesting scenario whilst activating features after then have been added to feature pack.

Consider this example code:

using (SPSite site = new SPSite("http://sharepintblog.com"))

{

        // get the site subscription

        SPSiteSubscription subscription = site.SiteSubscription;

 

        // create a new feature pack

        SPSiteSubscriptionSettingsManager mgr = SPSiteSubscriptionSettingsManager.Local;

        SPSiteSubscriptionFeaturePack fp = mgr.CreateFeaturePack();

 

        // assign the feature pack to the site subscription

        mgr.AssignFeaturePackToSiteSubscription(fp, subscription);

 

        // get an example feature – document set feature

        Guid featureID = new Guid("3BAE86A2-776D-499d-9DB8-FA4CDC7884F8");

 

        // get the feature definition

        SPFeatureDefinition featureDefinition = SPFarm.Local.FeatureDefinitions[featureID];

 

        // add the feature to the feature pack

        fp.Add(featureDefinition);

        fp.Update();

 

 

        // activate the feature

        switch (featureDefinition.Scope)

        {

            case SPFeatureScope.Site:

                site.Features.Add(featureDefinition.Id);

                break;

            case SPFeatureScope.Web:

                site.RootWeb.Features.Add(featureDefinition.Id);

                break;

        }

}

 

Basically, this code connects to a SharePoint site, gets its SPSiteSubscription object, creates a new Feature Pack, assigns the feature pack to the subscription, assigns a feature to the feature pack, updates the feature pack and then activates the feature on the site – Phew!

However, occasionally, the above code throws an error –

Failed to activate feature ‘DocumentSet’ (id: ‘3bae86a2-776d-499d-9db8-fa4cdc7884f8’).  The feature is not a member of the associated site subscription feature pack.

This issues is caused by the immediate activation of the feature on the site whilst is it still being added to the feature pack and made available to the site. This only happens occasionally but when it does the feature is left inactivated…

The work around I have discovered to this issue is to activate the feature in a separate SPSite instance. This way, the feature is available for activation before the site.Features.Add(featureDefinition.Id) occurs. The following code activates the feature every time!

Workaround:

// get an example feature – document set feature

Guid featureID = new Guid("3BAE86A2-776D-499d-9DB8-FA4CDC7884F8");

 

// get the feature definition

SPFeatureDefinition featureDefinition = SPFarm.Local.FeatureDefinitions[featureID];

// add the feature to the feature pack in the first SPSite object

using (SPSite site = new SPSite("http://sharepintblog.com"))

{

    // get the site subscription

    SPSiteSubscription subscription = site.SiteSubscription;

 

    // create a new feature pack

    SPSiteSubscriptionSettingsManager mgr = SPSiteSubscriptionSettingsManager.Local;

    SPSiteSubscriptionFeaturePack fp = mgr.CreateFeaturePack();

 

    // assign the feature pack to the site subscription

    mgr.AssignFeaturePackToSiteSubscription(fp, subscription);

 

    // add the feature to the feature pack

    fp.Add(featureDefinition);

    fp.Update();

}

 

// activate the feature in the second SPSite object

using (SPSite site = new SPSite("http://sharepintblog.com"))

{

    // activate the feature

    switch (featureDefinition.Scope)

    {

        case SPFeatureScope.Site:

            site.Features.Add(featureDefinition.Id);

            break;

        case SPFeatureScope.Web:

            site.RootWeb.Features.Add(featureDefinition.Id);

            break;

    }

}

 

I hope this helps…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: