Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have this code which works in a farm solution, but not in a sandbox solution.

Is it possible to do this with a sandbox solution?

The table in this link http://msdn.microsoft.com/en-us/library/ff798382.aspx doesnt give (me) the answer.

public class Feature1EventReceiver : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite siteCollection = properties.Feature.Parent as SPSite;
        if (siteCollection != null)
        {
            SPWeb topLevelSite = siteCollection.RootWeb;

            ActivateSharePointServerPublishingInfrastructureFeature(topLevelSite);
        }
    }

    private static void ActivateSharePointServerPublishingInfrastructureFeature(SPWeb topLevelSite)
    {
        // activating the Sharepoint Server publishing Infrastructure feature at the site collection
        string pubFeatureID = "F6924D36-2FA8-4f0b-B16D-06B7250180FA";
        Guid PublishingInfraFeatureID = new Guid(pubFeatureID);
        topLevelSite.Features.Add(PublishingInfraFeatureID, true, SPFeatureDefinitionScope.Site);
    }
}
share|improve this question
1  
I have to ask, why would you activate the publishing infrastructure after a feature has been activated via a receiver? This is the purpose of activation dependencies. Since the publishing infrastructure is not hidden you can simply add an activation dependency to your feature and get rid of this code all together. –  brian brinley May 5 '11 at 0:39
    
@brian - with your suggestion its likely I'm going down the wrong path! I have a sandbox solution that requires this feature to be activated and I didnt want to create another solution to activate the parent feature and this is how I started to do it. I shall look into activation dependencies. –  gotsp May 5 '11 at 1:36
add comment

3 Answers

According to MSDN, 'Yes': http://msdn.microsoft.com/en-us/library/ms456927.aspx

FYI, you can temporarily change your SharePoint.dll reference to this one to see if your code will compile:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\UserCode\assemblies\Microsoft.SharePoint.dll

share|improve this answer
add comment

I suspect that activating the publishing feature is trying to do something outside the given site collection, which sandbox solutions can not do.

I seem to remember there being some timer jobs that only get created when the first site collection that is a publishing site is added to a web application (something to do with content deployment i thought, its been a while, and it was 2007). The only timer job i can see in my current dev sharepoint farm that is missing when the web application is first created is "Enterprise Metadata site data update".

share|improve this answer
add comment
up vote 1 down vote accepted

With suggestion of @Brian I followed this article - http://karinebosch.wordpress.com/2011/03/12/feature-activation-dependencies-in-visual-studio-2010/

  1. Open the Feature Designer by double-clicking the feature in the Solution Explorer
  2. Scroll down in the Feature Designer until you encounter the Feature Activation Dependencies section.
  3. Click the + button to expand the Feature Activation Dependencies section.
  4. Click the Add button. This opens a dialog where you can select a feature on which you want to add a dependency.
  5. Add a custom dependency and added the following,

enter image description here

I also added this code to the FeatureActivated event to activate the site level publishing feature;

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite siteCollection = properties.Feature.Parent as SPSite;
        if (siteCollection != null)
        {
            SPWeb topLevelSite = siteCollection.RootWeb;

            string webAppRelativePath = GetRelativePathToSite(topLevelSite);

            // Enumerate through each site and apply branding.
            foreach (SPWeb site in siteCollection.AllWebs)
            {
                string sharePointServerPublishing = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb";
                Guid sharePointServerPublishingGuid = new Guid(sharePointServerPublishing);
                site.Features.Add(sharePointServerPublishingGuid, true);

                site.UIVersion = 4;
                site.Update();
            }
        }
    }

The sandbox solution has deployed successfully.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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