Can I activate the SharePoint Server publishing Infrastructure feature at the site collection level within a sandbox solution?

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/12210

  •  16-10-2019
  •  | 
  •  

Question

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 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);
    }
}
Was it helpful?

Solution 2

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.

OTHER TIPS

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".

According to MSDN, 'Yes'

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
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();
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top