private void DeployDefinition(object modelHost, SiteModelHost siteModelHost, DesignPackageDefinition definition)
        {
            var site = siteModelHost.HostSite;

            // TODO, implementation
            // Add DesignPackage provision support #166
            // https://github.com/SubPointSolutions/spmeta2/issues/166

            DesignPackageDefinition currrentArtifact = null;

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

            // TODO, implementation

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = currrentArtifact,
                ObjectType       = typeof(DesignPackageDefinition),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });
        }
 public static TModelNode AddDesignPackage <TModelNode>(this TModelNode model, DesignPackageDefinition definition,
                                                        Action <DesignPackageModelNode> action)
     where TModelNode : ModelNode, ISiteModelNode, new()
 {
     return(model.AddTypedDefinitionNode(definition, action));
 }
 public static TModelNode AddDesignPackage <TModelNode>(this TModelNode model, DesignPackageDefinition definition)
     where TModelNode : ModelNode, ISiteModelNode, new()
 {
     return(AddDesignPackage(model, definition, null));
 }
        private void DeployDesignPackage(object modelHost, SiteModelHost siteModelHost, DesignPackageDefinition definition)
        {
            var site            = siteModelHost.HostSite;
            var sandboxSolution = FindExistingSolutionById(siteModelHost, definition.SolutionId);

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

            if (sandboxSolution != null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                         "Processing existing design package");
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                         "Processing new design package");
            }

            var designPackageInfo = new DesignPackageInfo(definition.FileName,
                                                          definition.SolutionId,
                                                          definition.MajorVersion,
                                                          definition.MinorVersion);

            if (definition.Install)
            {
                // removing first
                if (sandboxSolution != null)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deleting existing sandbox solution");
                    siteModelHost.HostSite.Solutions.Remove(sandboxSolution);

                    var catalog = siteModelHost.HostSite.RootWeb.GetCatalog(SPListTemplateType.SolutionCatalog);

                    try
                    {
                        var solutionFile = catalog.RootFolder.Files[sandboxSolution.Name];

                        if (solutionFile.Exists)
                        {
                            solutionFile.Delete();
                        }
                    }
                    catch
                    {
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                                 "Error while deleting design package");
                    }
                }

                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying design package file to the root folder of the root web");

                var rootFolder           = site.RootWeb.RootFolder;
                var tmpDesignPackageFile = rootFolder.Files.Add(definition.FileName, definition.Content, true);

                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall,
                                     string.Format("Installing design package from URL:[{0}]", tmpDesignPackageFile.ServerRelativeUrl));

                DesignPackage.Install(site, designPackageInfo, tmpDesignPackageFile.ServerRelativeUrl);
                sandboxSolution = FindExistingSolutionById(siteModelHost, definition.SolutionId);

                if (definition.Apply)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Applying design package");
                    DesignPackage.Apply(site, designPackageInfo);
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Apply == false. Skipping design package activation");
                }

                // cleanup
                if (tmpDesignPackageFile.Exists)
                {
                    tmpDesignPackageFile.Delete();
                }

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

                if (sandboxSolution != null)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Solution is NOT NULL. Checking Apply status");

                    if (definition.Apply)
                    {
                        TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Applying design package");
                        DesignPackage.Apply(site, designPackageInfo);
                    }
                    else
                    {
                        TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall,
                                             "Apply == false. Skipping design package activation");
                    }
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Solution is NULL. Skipping Apply status");
                }

                sandboxSolution = FindExistingSolutionById(siteModelHost, definition.SolutionId);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = sandboxSolution,
                    ObjectType       = typeof(SPUserSolution),
                    ObjectDefinition = definition,
                    ModelHost        = modelHost
                });
            }
        }
        private void DeployArtifact(SiteModelHost modelHost, DesignPackageDefinition definition)
        {
            var site    = modelHost.HostSite;
            var context = site.Context;

            //var existingSolutionFile = FindExistingSolutionFile(siteModelHost, definition);
            var existingSolution = FindExistingSolutionById(modelHost, definition.SolutionId);

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

            // uninstalling existing one
            if (existingSolution != null && definition.Install)
            {
                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(definition.FileName))
                //    currentFileName = currentFileName.Replace(string.Format("-v{0}.{1}.wsp", 1, 0), string.Empty);
                //else
                //    currentFileName = definition.FileName;

                var packageFileName      = Path.GetFileNameWithoutExtension(currentFileName);
                var packageFileNameParts = packageFileName.Split('-');

                var packageName = packageFileNameParts[0];

                var versionParts = packageFileNameParts[1].Replace("v", string.Empty)
                                   .Split('.');

                var packageMajorVersion = int.Parse(versionParts[0]);
                var packageMinorVersion = int.Parse(versionParts[1]);

                var info = new DesignPackageInfo
                {
                    PackageGuid  = definition.SolutionId,
                    MajorVersion = packageMajorVersion,
                    MinorVersion = packageMinorVersion,
                    PackageName  = packageName,
                };

                // cleaning up AppliedDesignGuid at root web to enable 'force' uninstall
                var rootWeb = site.RootWeb;
                rootWeb.AllProperties["AppliedDesignGuid"] = null;
                rootWeb.Update();

                context.ExecuteQueryWithTrace();

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

                try
                {
                    context.ExecuteQueryWithTrace();
                }
                catch (Exception uninstallException)
                {
                    // pass this one
                    // bug in SharePoint's DesignPackage.UnInstall method
                    if (!uninstallException.ToString().Contains("33e33eca-7712-4f3d-ab83-6848789fc9b6"))
                    {
                        throw;
                    }
                }
            }

            // installing new
            if (definition.Install)
            {
                var folder = site.RootWeb.RootFolder;

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

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

                if (definition.Content.Length < 1024 * 1024 * 1.5)
                {
                    fileCreatingInfo.Content = definition.Content;
                }
                else
                {
                    fileCreatingInfo.ContentStream = new MemoryStream(definition.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();

                var info = new DesignPackageInfo
                {
                    PackageGuid  = definition.SolutionId,
                    MajorVersion = definition.MajorVersion,
                    MinorVersion = definition.MinorVersion,
                    PackageName  = Path.GetFileNameWithoutExtension(definition.FileName)
                };

                // activate and remove
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "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();

                existingSolution = FindExistingSolutionById(modelHost, definition.SolutionId);
            }

            if (definition.Apply)
            {
                var info = new DesignPackageInfo
                {
                    PackageGuid  = definition.SolutionId,
                    MajorVersion = definition.MajorVersion,
                    MinorVersion = definition.MinorVersion,
                    PackageName  = Path.GetFileNameWithoutExtension(definition.FileName)
                };

                // apply
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "DesignPackage.Apply()");
                DesignPackage.Apply(context, site, info);
                context.ExecuteQueryWithTrace();
            }

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