示例#1
0
        public override void AddPackage(AddPackageContext context, Boolean autoActivate)
        {
            SPWeb        targetWeb  = context.TargetWeb;
            SPSite       targetSite = targetWeb.Site;
            StorePackage package    = context.StorePackage;

            SPList solutionsList = targetSite.GetCatalog(SPListTemplateType.SolutionCatalog);

            bool currentWebUnsafeSettings = context.SPContext.Web.AllowUnsafeUpdates;
            bool currentSettings          = targetWeb.AllowUnsafeUpdates;

            targetWeb.AllowUnsafeUpdates             = true;
            context.SPContext.Web.AllowUnsafeUpdates = true;
            SPFile file = solutionsList.RootFolder.Files.Add(context.StorePackage.SolutionFileName, File.ReadAllBytes(context.SolutionFilePath));

            SPUserSolution solution = targetSite.Solutions.Add(file.Item.ID);

            targetWeb.AllowUnsafeUpdates             = currentSettings;
            context.SPContext.Web.AllowUnsafeUpdates = currentWebUnsafeSettings;

            if (!string.IsNullOrEmpty(package.SetupFeatureID))
            {
                if (autoActivate)
                {
                    try
                    {
                        SPSINStorePackageUtilities.AddPendingActivationFeature(context.TargetWeb, new Guid(package.SetupFeatureID));
                    }
                    catch
                    {
                        // Cannot add automatic activation...
                    }
                }
            }
        }
        public override void AddPackage(AddPackageContext context, Boolean autoActivate)
        {
            StorePackage package = context.StorePackage;

            try
            {
                Collection <SPWebApplication> webApps = new Collection <SPWebApplication>();
                webApps.Add(context.TargetWeb.Site.WebApplication);

                bool previousUnsafeUpdatesContextWeb = context.SPContext.Web.AllowUnsafeUpdates;
                context.SPContext.Web.AllowUnsafeUpdates = true;
                SPSolution solution = SPFarm.Local.Solutions.Add(context.SolutionFilePath);
                solution.Deploy(DateTime.Now, true, webApps, true);

                if (!string.IsNullOrEmpty(package.SetupFeatureID))
                {
                    if (autoActivate)
                    {
                        SPSINStorePackageUtilities.AddPendingActivationFeature(context.TargetWeb, new Guid(package.SetupFeatureID));
                    }
                }
                context.SPContext.Web.AllowUnsafeUpdates = previousUnsafeUpdatesContextWeb;
            }
            catch (Exception exc)
            {
                throw new SPException(exc.ToString());
            }
        }
        internal static void InstallPackageInWeb(StorePackage package, string targetWebURL)
        {
            SPContext context = SPContext.Current;

            using (SPSite site = new SPSite(targetWebURL))
            {
                using (SPWeb activationWeb = site.OpenWeb())
                {
                    WebClient wc               = new WebClient();
                    string    tempFileName     = Path.GetTempFileName();
                    string    solutionFileName = package.SolutionFileName;
                    tempFileName             = tempFileName.Replace(Path.GetFileName(tempFileName), solutionFileName);
                    wc.UseDefaultCredentials = true;
                    wc.DownloadFile(package.PackageURL, tempFileName);

                    AddPackageContext packageContext = new AddPackageContext();
                    packageContext.HttpContext      = HttpContext.Current;
                    packageContext.SolutionFilePath = tempFileName;
                    packageContext.SPContext        = SPContext.Current;
                    packageContext.StorePackage     = package;
                    packageContext.TargetWeb        = activationWeb;


                    if (package.SolutionType == SolutionType.Farm)
                    {
                        if (SPSINStorePackageUtilities.CanAddFarmSolutions(context, activationWeb))
                        {
                            SPSINStorePackageUtilities.AddFarmSolution(packageContext);
                        }
                    }
                    else if (package.SolutionType == SolutionType.Sandbox)
                    {
                        if (SPSINStorePackageUtilities.CanAddSandboxSolutions(SPContext.Current, activationWeb))
                        {
                            SPSINStorePackageUtilities.AddSandboxSolution(packageContext);
                        }
                    }
                    else
                    {
                        // use App type loader
                    }
                }
            }
        }