public void Can_Reverse_SandboxSolutions()
        {
            // only root web
            var options = ReverseOptions.Default
                            .AddDepthOption<WebDefinition>(0);

            // TODO
            // var title = Rnd.String();
            // options.AddFilterOption<SandboxSolutionDefinition>(d => d.Title == title);

            var wspPath = @"Content/Apps/SPMeta2.Containers.SandboxSolutionContainer.wsp";

            var sandboxSolutionDef = new SandboxSolutionDefinition
            {
                FileName = string.Format("{0}.wsp", Rnd.String()),
                Activate = true,

                SolutionId = new Guid("e9a61998-07f2-45e9-ae43-9e93fa6b11bb"),

                Content = System.IO.File.ReadAllBytes(wspPath)
            };

            var model = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddSandboxSolution(sandboxSolutionDef);
            });

            DeployReverseAndTestModel(model, options);
        }
Example #2
0
        public void CanDeploy_SandboxWebFeature()
        {
            var sandboxSolution = new SandboxSolutionDefinition()
            {
                FileName = string.Format("{0}.wsp", Rnd.String()),
                Activate = true,

                SolutionId = new Guid("e9a61998-07f2-45e9-ae43-9e93fa6b11bb"),
                Content = ModuleFileUtils.FromResource(GetType().Assembly, "SPMeta2.Regression.Tests.Templates.SandboxSolutions.SPMeta2.Containers.SandboxSolutionContainer.wsp")
            };

            var siteModel = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddSandboxSolution(sandboxSolution);
            });

            var model = SPMeta2Model
                .NewWebModel(web =>
                {
                    web.AddFeature(new FeatureDefinition
                    {
                        Enable = true,
                        ForceActivate = true,
                        Id = new Guid("b997a462-8efb-44cf-92c0-457e75c81798"),
                        Scope = FeatureDefinitionScope.Web
                    });
                });

            TestModel(siteModel, model);
        }
        public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options)
        {
            var item = (reverseHost as SandboxSolutionReverseHost).HostSandboxSolutionFile;
            var context = (reverseHost as SandboxSolutionReverseHost).HostClientContext;

            var def = new SandboxSolutionDefinition();

            def.FileName = item.Name;
            def.SolutionId = ConvertUtils.ToGuid(item.ListItemAllFields["SolutionId"]).Value;

            def.Activate = true;

            using (var stream = File.OpenBinaryDirect(
                context,
                item.ServerRelativeUrl).Stream)
            {
                def.Content = ModuleFileUtils.ReadFully(stream);
            }

            return new SandboxSolutionModelNode
            {
                Options = { RequireSelfProcessing = true },
                Value = def
            };
        }
Example #4
0
        public void CanDeploy_SandboxSiteFeature()
        {
            var sandboxSolution = new SandboxSolutionDefinition()
            {
                FileName = string.Format("{0}.wsp", Rnd.String()),
                Activate = true,

                SolutionId = new Guid("e9a61998-07f2-45e9-ae43-9e93fa6b11bb"),
                Content = ModuleFileUtils.FromResource(GetType().Assembly, "SPMeta2.Regression.Tests.Templates.SandboxSolutions.SPMeta2.Containers.SandboxSolutionContainer.wsp")
            };

            var model = SPMeta2Model
                .NewSiteModel(site =>
                {
                    site.AddSandboxSolution(sandboxSolution);

                    site.AddFeature(new FeatureDefinition
                    {
                        Enable = true,
                        ForceActivate = true,
                        Id = new Guid("32dc6bed-0298-4fca-a72f-e9b9c0d6f09a"),
                        Scope = FeatureDefinitionScope.Site
                    });
                });

            TestModel(model);
        }
        private void ValidateDefinition(SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            if (!sandboxSolutionDefinition.Activate)
                throw new SPMeta2NotSupportedException("SandboxSolutionDefinition.Activate must be true. (DesignPackage API requires it).");

            if (sandboxSolutionDefinition.SolutionId == default(Guid))
                throw new SPMeta2NotSupportedException("SandboxSolutionDefinition.SolutionId must be defined for CSOM based provision (DesignPackage API requires it).");

            var fileName = Path.GetFileNameWithoutExtension(sandboxSolutionDefinition.FileName);

            if (fileName.Contains("."))
                throw new SPMeta2NotSupportedException("SandboxSolutionDefinition.FileName must not contain dots. (DesignPackage API requires it).");
        }
Example #6
0
        public void CanDeploy_List_From_CustomTemplate()
        {
            var sandboxSolution = new SandboxSolutionDefinition()
            {
                FileName = string.Format("{0}.wsp", Rnd.String()),
                Activate = true,

                SolutionId = new Guid("e9a61998-07f2-45e9-ae43-9e93fa6b11bb"),
                Content = ModuleFileUtils.FromResource(GetType().Assembly, "SPMeta2.Regression.Tests.Templates.SandboxSolutions.SPMeta2.Containers.SandboxSolutionContainer.wsp")
            };

            var customListDef = ModelGeneratorService.GetRandomDefinition<ListDefinition>(def =>
            {
                def.TemplateType = BuiltInListTemplateTypeId.GenericList;
                def.ForceCheckout = false;

                def.Url = Rnd.String();
                def.CustomUrl = string.Empty;

                // would give invalid list template exception
                //def.TemplateType = 10500;

                // works well, just reset TemplateType as GetRandomDefinition<> generated random TemplateType
                def.TemplateType = 0;
                def.TemplateName = "CustomerList";
            });

            var siteModel = SPMeta2Model
                .NewSiteModel(site =>
                {
                    site.AddSandboxSolution(sandboxSolution);
                });

            var webModel = SPMeta2Model
                .NewWebModel(web =>
                {
                    web.AddRandomWeb(subWeb =>
                    {
                        subWeb.AddWebFeature(new FeatureDefinition
                        {
                            Id = new Guid("b997a462-8efb-44cf-92c0-457e75c81798"),
                            Scope = FeatureDefinitionScope.Web,
                            Enable = true,
                            ForceActivate = true
                        });

                        subWeb.AddList(customListDef);
                    });

                });

            TestModel(siteModel, webModel);
        }
        protected SPUserSolution FindExistingSolution(SiteModelHost siteModelHost,
            SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving sandbox solution by SolutionId: [{0}]", sandboxSolutionDefinition.SolutionId);

            return siteModelHost.HostSite.Solutions.OfType<SPUserSolution>()
                                                                      .FirstOrDefault(f => f.SolutionId == sandboxSolutionDefinition.SolutionId);
        }
        private void DeploySandboxSolution(object modelHost, SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            var existingSolutions = FindExistingSolutionFile(siteModelHost, sandboxSolutionDefinition);
            var sandboxSolution = FindExistingSolution(siteModelHost, sandboxSolutionDefinition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = sandboxSolution,
                ObjectType = typeof(SPUserSolution),
                ObjectDefinition = sandboxSolutionDefinition,
                ModelHost = modelHost
            });

            if (sandboxSolution != null)
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing sandbox solution");

            if (sandboxSolution != null && sandboxSolution.Status == SPUserSolutionStatus.Activated)
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deleting existing sandbox solution");
                siteModelHost.HostSite.Solutions.Remove(sandboxSolution);
            }

            var solutionGallery = (SPDocumentLibrary)siteModelHost.HostSite.GetCatalog(SPListTemplateType.SolutionCatalog);
            var file = solutionGallery.RootFolder.Files.Add(sandboxSolutionDefinition.FileName, sandboxSolutionDefinition.Content, true);

            if (sandboxSolutionDefinition.Activate)
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Activating sandbox solution");

                var userSolution = siteModelHost.HostSite.Solutions.Add(file.Item.ID);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = userSolution,
                    ObjectType = typeof(SPUserSolution),
                    ObjectDefinition = sandboxSolutionDefinition,
                    ModelHost = modelHost
                });
            }
            else
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Activate = false. Continue provision");

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = sandboxSolution,
                    ObjectType = typeof(SPUserSolution),
                    ObjectDefinition = sandboxSolutionDefinition,
                    ModelHost = modelHost
                });
            }
        }
        protected SPFile FindExistingSolutionFile(SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving sandbox solution by FileName: [{0}]", sandboxSolutionDefinition.FileName);


            var site = siteModelHost.HostSite;
            var solutionGallery = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);

            return solutionGallery.RootFolder
                .Files
                .OfType<SPFile>()
                .FirstOrDefault(f => f.Name.ToUpper() == sandboxSolutionDefinition.FileName.ToUpper());
        }
Example #10
0
        public void CanDeploy_Custom_WebTemplate_As_SaveAsTemplate()
        {
            // TODO
            // should be uploaded manually yet
            var solution = new SandboxSolutionDefinition
            {
                FileName = Rnd.WspFileName(),
                Content = File.ReadAllBytes(DefaultContainers.WebTemplates.M2CustomWebAsTemplate.FilePath),
                Activate = true,
                SolutionId = DefaultContainers.WebTemplates.M2CustomWebAsTemplate.SolutionId
            };

            var mainWeb = ModelGeneratorService.GetRandomDefinition<WebDefinition>(def =>
            {
                def.WebTemplate = string.Empty;
                def.CustomWebTemplate = DefaultContainers.WebTemplates.M2CustomWebAsTemplate.WebTemplateName;
            });

            var subWeb = ModelGeneratorService.GetRandomDefinition<WebDefinition>(def =>
            {
                def.WebTemplate = string.Empty;
                def.CustomWebTemplate = DefaultContainers.WebTemplates.M2CustomWebAsTemplate.WebTemplateName;
            });

            var siteModel = SPMeta2Model.NewSiteModel(site =>
            {
                // required by the team site template
                // might not be eabled due to publishing site collection
                var targetSiteFeatureIds = new[]
                {
                    new Guid("14aafd3a-fcb9-4bb7-9ad7-d8e36b663bbd"),
                    new Guid("b21b090c-c796-4b0f-ac0f-7ef1659c20ae"),
                    new Guid("{5f3b0127-2f1d-4cfd-8dd2-85ad1fb00bfc}"),
                    new Guid("{2ed1c45e-a73b-4779-ae81-1524e4de467a}"),
                    new Guid("{39d18bbf-6e0f-4321-8f16-4e3b51212393}"),

                    new Guid("{4248e21f-a816-4c88-8cab-79d82201da7b}"),
                    new Guid("{43f41342-1a37-4372-8ca0-b44d881e4434}"),
                    new Guid("{5a979115-6b71-45a5-9881-cdc872051a69}"),
                    new Guid("{3cb475e7-4e87-45eb-a1f3-db96ad7cf313}"),
                    new Guid("{4c42ab64-55af-4c7c-986a-ac216a6e0c0e}"),
                    new Guid("{9fec40ea-a949-407d-be09-6cba26470a0c}"),
                    new Guid("{875d1044-c0cf-4244-8865-d2a0039c2a49}"),
                    new Guid("{5eac763d-fbf5-4d6f-a76b-eded7dd7b0a5}"),
                    new Guid("{6e8f2b8d-d765-4e69-84ea-5702574c11d6}"),
                    new Guid("{744b5fd3-3b09-4da6-9bd1-de18315b045d}"),

                    new Guid("{8581a8a7-cf16-4770-ac54-260265ddb0b2}"),
                    new Guid("{2fcd5f8a-26b7-4a6a-9755-918566dba90a}"),
                    new Guid("{e995e28b-9ba8-4668-9933-cf5c146d7a9f}"),

                    //new Guid("{c9c9515d-e4e2-4001-9050-74f980f93160}"),

                };

                foreach (var featureId in targetSiteFeatureIds)
                {
                    site.AddSiteFeature(new FeatureDefinition
                    {
                        Id = featureId,
                        Enable = true,
                        Scope = FeatureDefinitionScope.Site
                    });
                }

                //site.AddSandboxSolution(solution);
            });

            var model = SPMeta2Model.NewWebModel(rootWeb =>
            {
                rootWeb.AddWeb(mainWeb, web =>
                {
                    web.AddWeb(subWeb);
                });
            });

            TestModel(siteModel, model);
        }
        private void DeploySandboxSolution(object modelHost, SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            var site = siteModelHost.HostSite;
            var context = site.Context;

            var existingSolutionFile = FindExistingSolutionFile(siteModelHost, sandboxSolutionDefinition);
            var existingSolution = FindExistingSolution(siteModelHost, sandboxSolutionDefinition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = existingSolutionFile,
                ObjectType = typeof(File),
                ObjectDefinition = sandboxSolutionDefinition,
                ModelHost = modelHost
            });

            if (existingSolution != null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing sandbox solution");

                var fileItem = existingSolution.ListItemAllFields;
                context.Load(fileItem);
                context.ExecuteQueryWithTrace();

                var currentFileName = fileItem["FileLeafRef"].ToString();

                if (!currentFileName.Contains(sandboxSolutionDefinition.FileName))
                {
                    currentFileName = currentFileName.Replace(string.Format("-v{0}.{1}.wsp", 1, 0), string.Empty);
                }
                else
                {
                    currentFileName = sandboxSolutionDefinition.FileName;
                }

                var info = new DesignPackageInfo
                {
                    PackageGuid = sandboxSolutionDefinition.SolutionId,
                    MajorVersion = 1,
                    MinorVersion = 0,
                    PackageName = Path.GetFileNameWithoutExtension(currentFileName)
                };

                // deactivate and remove
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deleting existing sandbox solution via DesignPackage.UnInstall()");
                DesignPackage.UnInstall(context, site, info);

                context.ExecuteQueryWithTrace();
            }

            //var solutionGallery = site.RootWeb.Lists.GetByTitle("Site Assets");
            //var folder = solutionGallery.RootFolder;
            var folder = site.RootWeb.RootFolder;

            context.Load(folder);
            context.ExecuteQueryWithTrace();

            var fileCreatingInfo = new FileCreationInformation
            {
                Url = sandboxSolutionDefinition.FileName,
                Overwrite = true
            };

            if (sandboxSolutionDefinition.Content.Length < 1024 * 1024 * 1.5)
            {
                fileCreatingInfo.Content = sandboxSolutionDefinition.Content;
            }
            else
            {
                fileCreatingInfo.ContentStream = new System.IO.MemoryStream(sandboxSolutionDefinition.Content);
            }


            var newFile = folder.Files.Add(fileCreatingInfo);

            context.Load(newFile);
            context.Load(newFile, f => f.ServerRelativeUrl);

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Uploading new sandbox solution to root site folder");
            context.ExecuteQueryWithTrace();

            if (sandboxSolutionDefinition.Activate)
            {
                var info = new DesignPackageInfo
                {
                    PackageGuid = sandboxSolutionDefinition.SolutionId,
                    MajorVersion = 1,
                    MinorVersion = 0,
                    PackageName = Path.GetFileNameWithoutExtension(sandboxSolutionDefinition.FileName)
                };

                // activate and remove
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Activating sandbox solution via DesignPackage.Install()");
                DesignPackage.Install(context, site, info, newFile.ServerRelativeUrl);
                context.ExecuteQueryWithTrace();

                // clean up the file
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deleting sandbox file from root site folder");
                newFile.DeleteObject();
                context.ExecuteQueryWithTrace();
            }

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = newFile,
                ObjectType = typeof(File),
                ObjectDefinition = sandboxSolutionDefinition,
                ModelHost = modelHost
            });
        }
        protected File FindExistingSolutionFile(SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving sandbox solution by FileName: [{0}]", sandboxSolutionDefinition.FileName);

            var site = siteModelHost.HostSite;
            var context = site.Context;

            var solutionGallery = site.GetCatalog((int)ListTemplateType.SolutionCatalog);
            var files = solutionGallery.GetItems(CamlQuery.CreateAllItemsQuery());

            context.Load(files);
            context.ExecuteQueryWithTrace();

            var fileItem = files.ToList().FirstOrDefault(f => f["FileLeafRef"].ToString() == sandboxSolutionDefinition.FileName);

            return fileItem != null ? fileItem.File : null;
        }
 public static ModelNode AddSandboxSolution(this ModelNode model, SandboxSolutionDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddSandboxSolution(this ModelNode model, SandboxSolutionDefinition definition)
 {
     return AddSandboxSolution(model, definition, null);
 }