Example #1
0
        protected ClientObjectList<AppInstance> FindExistingApps(WebModelHost webHost, AppDefinition appModel)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "FindExistingApps() - finding app by productId: [{0}]", appModel.ProductId);

            var context = webHost.HostWeb.Context;
            var result = webHost.HostWeb.GetAppInstancesByProductId(appModel.ProductId);

            context.Load(result);
            context.ExecuteQueryWithTrace();

            return result;
        }
Example #2
0
 public static ModelNode AddApp(this ModelNode model, AppDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
Example #3
0
 public static ModelNode AddApp(this ModelNode model, AppDefinition definition)
 {
     return AddApp(model, definition, null);
 }
Example #4
0
        private void DeployApp(object modelHost, WebModelHost webHost, AppDefinition appModel)
        {
            var web = webHost.HostWeb;
            Guid appId = Guid.Empty;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Creating memory stream on appModel.Content");


            using (var appPackage = new MemoryStream(appModel.Content))
            {
                var currentApplications = FindExistingApps(webHost, appModel);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = currentApplications.FirstOrDefault(),
                    ObjectType = typeof(SPAppInstance),
                    ObjectDefinition = appModel,
                    ModelHost = modelHost
                });

                if (currentApplications == null || currentApplications.Count == 0)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Cannot find application by productId. Loading and installing new instance.");

                    // install new
                    var newAppInstance = web.LoadAndInstallApp(appPackage);

                    if (newAppInstance != null && newAppInstance.Status == SPAppInstanceStatus.Initialized)
                    {
                        appId = newAppInstance.Id;

                        var count = 0;
                        SPAppInstance localInstance = null;

                        do
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                               "Waiting while app is being installed for [{0}] milliseconds.",
                               WaitTimeInMillliseconds);

                            Thread.Sleep(WaitTimeInMillliseconds);
                            localInstance = web.GetAppInstanceById(appId);

                            count++;
                        } while (localInstance != null &&
                                 localInstance.Status != SPAppInstanceStatus.Installed &&
                                 count < MaxInstallAttempCount);


                    }

                    newAppInstance = web.GetAppInstanceById(appId);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = newAppInstance,
                        ObjectType = typeof(SPAppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing application");

                    for (int i = 0; i < currentApplications.Count; i++)
                    {
                        var upApp = currentApplications[i];
                        var upVersion = new Version(upApp.App.VersionString);

                        var targetVersion = new Version(appModel.Version);

                        if (upVersion < targetVersion)
                            currentApplications[i].Upgrade(appPackage);
                    }

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentApplications.FirstOrDefault(),
                        ObjectType = typeof(SPAppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
            }
        }
Example #5
0
 protected IList<SPAppInstance> FindExistingApps(WebModelHost webHost, AppDefinition appModel)
 {
     return webHost.HostWeb.GetAppInstancesByProductId(appModel.ProductId);
 }
Example #6
0
        private void DeployApp(object modelHost, WebModelHost webHost, AppDefinition appModel)
        {
            var web = webHost.HostWeb;
            Guid appId = Guid.Empty;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Creating memory stream on appModel.Content");


            using (var appPackage = new MemoryStream(appModel.Content))
            {
                var currentApplications = FindExistingApps(webHost, appModel);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = currentApplications.FirstOrDefault(),
                    ObjectType = typeof(SPAppInstance),
                    ObjectDefinition = appModel,
                    ModelHost = modelHost
                });

                if (currentApplications.Count == 0)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Cannot find application by productId. Loading and installing new instance.");

                    // install new
                    var newAppInstance = web.LoadAndInstallApp(appPackage);

                    if (newAppInstance != null && newAppInstance.Status == SPAppInstanceStatus.Initialized)
                    {


                        appId = newAppInstance.Id;

                        var count = 0;
                        SPAppInstance localInstance = null;

                        do
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                               "Waiting while app is being installed for [{0}] milliseconds.",
                               WaitTimeInMillliseconds);

                            Thread.Sleep(WaitTimeInMillliseconds);
                            localInstance = web.GetAppInstanceById(appId);

                            count++;
                        } while (localInstance != null &&
                                 localInstance.Status != SPAppInstanceStatus.Installed &&
                                 count < MaxInstallAttempCount);


                    }

                    newAppInstance = web.GetAppInstanceById(appId);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = newAppInstance,
                        ObjectType = typeof(SPAppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    //we had check early
                    var currentApp = currentApplications.FirstOrDefault();

                    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                        string.Format("Processing existing application. Upgrading from [{0}] to [{1}]", currentApp.App.VersionString, appModel.Version));

                    var hasUpdate = false;

                    for (int i = 0; i < currentApplications.Count; i++)
                    {
                        var spApp = currentApplications[i];
                        var spAppVersion = new Version(spApp.App.VersionString);

                        var definitionVersion = new Version(appModel.Version);

                        if (definitionVersion > spAppVersion)
                        {
                            TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Performing upgrade");

                            var updateAppInstance = currentApplications[i];
                            var updateAppId = updateAppInstance.Id;

                            var count = 0;
                            SPAppInstance localUpdateAppInstance = null;

                            updateAppInstance.Upgrade(appPackage);

                            do
                            {
                                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                                    "Waiting while app is being installed for [{0}] milliseconds.",
                                    WaitTimeInMillliseconds);

                                Thread.Sleep(WaitTimeInMillliseconds);
                                localUpdateAppInstance = web.GetAppInstanceById(updateAppId);

                                count++;
                            } while (localUpdateAppInstance != null &&
                                     localUpdateAppInstance.Status != SPAppInstanceStatus.Installed &&
                                     count < MaxInstallAttempCount);

                            hasUpdate = true;
                        }
                        else
                        {
                            TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Skipping upgrade due to the lower version");
                        }
                    }

                    if (hasUpdate)
                    {
                        // refreshing the app collection after update
                        // the .App.VersionString property will be refreshed
                        currentApplications = FindExistingApps(webHost, appModel);
                    }

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentApplications.FirstOrDefault(),
                        ObjectType = typeof(SPAppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
            }
        }
Example #7
0
        private void AddAndCheckRandomAppWithVersion(WebModelNode web, Version version)
        {
            var def = new AppDefinition();

            def.ProductId = DefaultContainers.Apps.ProductId;
            def.Version = version.ToString();

            def.Content = File.ReadAllBytes(string.Format(DefaultContainers.Apps.GenericVersionableAppFilePath, version));

            web.AddApp(def, app =>
            {
                app.OnProvisioned<object, AppDefinition>(context =>
                {
                    Trace.WriteLine(context.ObjectDefinition.ToString());
                    Trace.WriteLine(context.Object.ToString());

                    var expectedAppVersion = new Version(context.ObjectDefinition.Version);

                    var obj = context.Object;
                    var objType = context.Object.GetType();

                    if (objType.ToString().Contains("Microsoft.SharePoint.Client.AppInstance"))
                    {
                        // with CSOM there is no API to know current app installed version
                        // checking if app is Installed after every single update
                        var appStatus = obj.GetPropertyValue("Status").ToString();

                        Assert.IsTrue(appStatus == "Installed",
                            string.Format("App should be installed after every update"));
                    }
                    else if (objType.ToString().Contains("Microsoft.SharePoint.Administration.SPAppInstance"))
                    {
                        var appObjet = obj.GetPropertyValue("App");

                        var versionString = appObjet.GetPropertyValue("VersionString") as string;
                        var spAppVersion = new Version(versionString);

                        // either equal (update) or SharePoint version greater than local (second update)
                        // the test is run several times, so only once we have =, and then we have <
                        Assert.IsTrue(expectedAppVersion <= spAppVersion,
                            string.Format("Expecting app version:[{0}] SharePoint app version:[{1}]", expectedAppVersion, spAppVersion));
                    }
                    else
                    {
                        throw new SPMeta2NotImplementedException(string.Format("ID property extraction is not implemented for type: [{0}]", objType));
                    }
                });
            });
        }
Example #8
0
        private void DeployApp(object modelHost, WebModelHost webHost, AppDefinition appModel)
        {
            var web = webHost.HostWeb;
            var context = web.Context;

            var appId = Guid.Empty;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Creating memory stream on appModel.Content");

            using (var appPackage = new MemoryStream(appModel.Content))
            {
                var currentApplications = FindExistingApps(webHost, appModel);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = currentApplications.FirstOrDefault(),
                    ObjectType = typeof(AppInstance),
                    ObjectDefinition = appModel,
                    ModelHost = modelHost
                });


                if (currentApplications == null || currentApplications.Count == 0)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Cannot find application by productId. Loading and installing new instance.");

                    // install new
                    var newAppInstance = web.LoadAndInstallApp(appPackage);



                    context.Load(newAppInstance);
                    context.ExecuteQueryWithTrace();

                    if (newAppInstance != null && newAppInstance.Status == AppInstanceStatus.Initialized)
                    {
                        appId = newAppInstance.Id;

                        var count = 0;
                        AppInstance localInstance = null;

                        do
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                                "Waiting while app is being installed for [{0}] milliseconds.",
                                WaitTimeInMillliseconds);

                            Thread.Sleep(WaitTimeInMillliseconds);
                            localInstance = web.GetAppInstanceById(appId);

                            context.Load(localInstance, l => l.Status);
                            context.ExecuteQueryWithTrace();

                            count++;
                        } while (localInstance != null &&
                                 localInstance.Status != AppInstanceStatus.Installed &&
                                 count < MaxInstallAttempCount);
                    }

                    newAppInstance = web.GetAppInstanceById(appId);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = newAppInstance,
                        ObjectType = typeof(AppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    //we had check early
                    var currentApp = currentApplications.FirstOrDefault();

                    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                        string.Format("Processing existing application. Upgrading from [{0}] to [{1}]", currentApp.Title, appModel.Version));

                    var hasUpdate = false;

                    for (int i = 0; i < currentApplications.Count; i++)
                    {
                        var spApp = currentApplications[i];
                        //var spAppVersion = new Version(spApp.App.VersionString);

                        var definitionVersion = new Version(appModel.Version);

                        // always install
                        //if (definitionVersion > spAppVersion)
                        //{
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Performing upgrade");

                        var updateAppInstance = currentApplications[i];
                        var updateAppId = updateAppInstance.Id;

                        var count = 0;
                        AppInstance localUpdateAppInstance = null;

                        try
                        {
                            updateAppInstance.Upgrade(appPackage);
                            context.ExecuteQueryWithTrace();
                        }
                        catch (Exception upgradeException)
                        {
                            // handling early version upgrades
                            // Microsoft.SharePoint.Client.ServerException]	
                            // {"An App Instance can only be upgraded to a newer version of the same product. The upgrade request was for product 1.0.0.3 version e81b6820-5d57-4d17-a098-5f4317f6c400 to product 1.0.0.0 version e81b6820-5d57-4d17-a098-5f4317f6c400."}

                            if (IsAppUpgradeException(upgradeException))
                            {
                                // fascinating
                                // jumping to the happy end
                                goto AppShouldNotBeUpdated;
                            }

                            throw;
                        }

                        do
                        {
                            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall,
                                "Waiting while app is being installed for [{0}] milliseconds.",
                                WaitTimeInMillliseconds);

                            Thread.Sleep(WaitTimeInMillliseconds);
                            localUpdateAppInstance = web.GetAppInstanceById(updateAppId);

                            context.Load(localUpdateAppInstance, l => l.Status);
                            context.ExecuteQueryWithTrace();

                            count++;
                        } while (localUpdateAppInstance != null &&
                                 localUpdateAppInstance.Status != AppInstanceStatus.Installed &&
                                 count < MaxInstallAttempCount);

                        hasUpdate = true;

                        //}
                        //else
                        //{
                        //    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Skipping upgrade due to the lower version");
                        //}
                    }

                    if (hasUpdate)
                    {
                        // refreshing the app collection after update
                        // the .App.VersionString property will be refreshed
                        currentApplications = FindExistingApps(webHost, appModel);
                    }

                AppShouldNotBeUpdated:

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentApplications.FirstOrDefault(),
                        ObjectType = typeof(AppInstance),
                        ObjectDefinition = appModel,
                        ModelHost = modelHost
                    });
                }
            }
        }