0

I'm deploying a custom Site Template (wsp) through Visual Studio with the code shown below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="IRBaseTemplate" List="111" Url="_catalogs/solutions">
    <File Url="IRBaseTemplate/IRBaseTemplate.wsp" Path="IRBaseTemplate\IRBaseTemplate.wsp" Type="GhostableInLibrary">
      <Property Name="LanguageDisplay" Value="English" />
      <Property Name="Version" Value="4" />
    </File>
  </Module>
</Elements>

This works and deploys the wsp to the solutions gallery. Does anybody know how to activate / deactivate this solution so I don't have to do it manually? Bonus points if you know how to Delete it.

1 Answer 1

0

Try the below code to activate sandbox. It needs FeatureActivated event

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    string siteURL = "http://sitecollectionurl/";

    SPSite spSite = new SPSite(siteURL);

    SPUserToken systemToken = spSite.SystemAccount.UserToken;

    //impersonating user 
    using (SPSite site = new SPSite(siteURL, systemToken))
    {
        // Get all names of added solutions
        List solutionName = new List();
        SPUserSolutionCollection solutionsCollection = site.Solutions;

        foreach (SPUserSolution solution in site.Solutions)
        {
            string name = solution.Name;
            if (!solutionName.Contains(name))
            {
                solutionName.Add(name);
            }
        }

        // Add new solutions to the collection of the gallery
        SPDocumentLibrary solutionsGallery = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);
        foreach (SPListItem item in solutionsGallery.Items)
        {
            if (!solutionName.Contains(item.Name))
            {
                solutionsCollection.Add(item.ID);
            }
        }

        // Properly activate solutions
        SPFeatureCollection featureCollection = site.Features;
        foreach (SPUserSolution solution in solutionsCollection)
        {
            Guid solutionId = solution.SolutionId;

            SPFeatureDefinitionCollection siteFeatures = site.FeatureDefinitions;

            var features = from SPFeatureDefinition f in siteFeatures
            where f.SolutionId.Equals(solutionId) && f.Scope == SPFeatureScope.Site
            select f;

            foreach (SPFeatureDefinition featureDefinition in features)
            {
                SPFeature foundFeature = featureCollection[featureDefinition.Id];

                if (foundFeature == null)
                site.Features.Add(featureDefinition.Id, false, SPFeatureDefinitionScope.Site);
            }
        }

    }
}

To deactivate it, you need to write it inside FeatureDeactivating event

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    ...above entire code and then replace the last foreach as below
    foreach (SPFeatureDefinition featureDefinition in features)
    {
        SPFeature foundFeature = featureCollection[featureDefinition.Id];

        site.Features.Remove(featureDefinition.Id,true);
    }
}

Reference - Activating SharePoint Sandboxed Solutions

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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