Activating SharePoint 2010 SandBox Solutions aka User Solutions Programmatically


It seems SPUserSolutionCollection.Add though changes the status of a User Solution to 'Activated' but the Site Collection(Site) Level Features aren't activated.
Other methods of activating a solution , like UI, Powershell do not exhibit this behavior. In other words, if a WSP was activated from User Solutions Gallery or via Powershell, it's site level Features will be activated.
Following workaround however can help.
The workaround is to mimic what the other two ways of activating a User Solution do, that is calling an internal function EnsureSiteCollectionFeaturesActivated of Microsoft.SharePoint.SPUserSolutionCollection class.
We need to create this function in our code and call it after activating the solution via Microsoft.SharePoint.SPUserSolutionCollection.Add.
CODE demonstrating the workaround :

static void Main(string[] args)
        {
            using (SPSite oSite = new SPSite("http://intranet"))
            {
                using (SPWeb oweb = oSite.OpenWeb())
                {
                    SPWeb rootweb = oweb.Site.RootWeb;
                    SPList solGallery = rootweb.Site.GetCatalog(SPListTemplateType.SolutionCatalog);
                    foreach (SPListItem item in solGallery.Items)
                    {
                        if (item.File.Name.Equals("TestWSpSav2Template.wsp"))
                        {
                            SPUserSolution solutiontoActivate = rootweb.Site.Solutions.Add(item.ID);
                            EnsureSiteCollectionFeaturesActivated(solutiontoActivate, oSite);
                            break;
                        }
                    }
                }
            }
        }
        static void EnsureSiteCollectionFeaturesActivated(SPUserSolution solution, SPSite site)
        {
            SPUserSolutionCollection solutions = site.Solutions;
            List<SPFeatureDefinition> oListofFeatures = GetFeatureDefinitionsInSolution(solution, site);
            foreach (SPFeatureDefinition def in oListofFeatures)
            {
                if (((def.Scope == SPFeatureScope.Site) && def.ActivateOnDefault) && (site.Features[def.Id] == null))
                {
                    site.Features.Add(def.Id, false, SPFeatureDefinitionScope.Site);
                }
            }
        }
        static List<SPFeatureDefinition> GetFeatureDefinitionsInSolution(SPUserSolution solution, SPSite site)
        {
            List<SPFeatureDefinition> list = new List<SPFeatureDefinition>();
            foreach (SPFeatureDefinition definition in site.FeatureDefinitions)
            {
                if (definition.SolutionId.Equals(solution.SolutionId))
                {
                    list.Add(definition);
                }
            }
            return list;
        }

Comments (1)
  1. Shamshad Ali says:

    Hi,

    Could you please help me with following issue i posted on technet:

    social.technet.microsoft.com/…/9c9e0203-fee2-4b1e-8e3c-d07774b26863

    Please help !!!

    Shamshad Ali

Comments are closed.

Skip to main content