Esempio n. 1
0
        public void AddCheckRemoveAppTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                AppManager manager  = new AppManager(clientContext);
                var        appBytes = OfficeDevPnP.Core.Tests.Properties.Resources.alm;

                //Test adding app
                var addedApp = manager.Add(appBytes, $"app-{appGuid}.sppkg", true);

                Assert.IsNotNull(addedApp);

                //Test availability of apps
                var availableApps = manager.GetAvailable();

                Assert.IsNotNull(availableApps);
                CollectionAssert.Contains(availableApps.Select(app => app.Id).ToList(), addedApp.Id);

                var retrievedApp = manager.GetAvailable(addedApp.Id);
                Assert.AreEqual(addedApp.Id, retrievedApp.Id);

                //Test removal
                var removeResults = manager.Remove(addedApp.Id);

                Assert.IsTrue(removeResults);
            }
        }
Esempio n. 2
0
        public void DeployRetractAppTest()
        {
            TestCommon.RegisterPnPHttpClientMock();
            using (var clientContext = TestCommon.CreateTestClientContext())
            {
                AppManager manager  = new AppManager(clientContext);
                var        appBytes = Resources.almskip;

                var results = manager.Add(appBytes, $"appalmskip-{appGuid}.sppkg", true);

                var deployResults = manager.Deploy(results.Id);
                Assert.IsTrue(deployResults);

                var metadata = manager.GetAvailable(results.Id);
                Assert.IsTrue(metadata.Deployed);

                var retractResults = manager.Retract(results.Id);
                Assert.IsTrue(retractResults);

                metadata = manager.GetAvailable(results.Id);
                Assert.IsFalse(metadata.Deployed);

                manager.Remove(results.Id);
            }
        }
Esempio n. 3
0
        protected override void ExecuteCmdlet()
        {
            if (!System.IO.Path.IsPathRooted(Path))
            {
                Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
            }

            var fileInfo = new System.IO.FileInfo(Path);

            var bytes = System.IO.File.ReadAllBytes(Path);

            var manager = new AppManager(ClientContext);

            var result = manager.Add(bytes, fileInfo.Name, Overwrite, Scope, timeoutSeconds: Timeout);

            try
            {
                if (Publish)
                {
                    if (manager.Deploy(result, SkipFeatureDeployment, Scope))
                    {
                        result = manager.GetAvailable(result.Id, Scope);
                    }
                }
                WriteObject(result);
            }
            catch
            {
                // Exception occurred rolling back
                manager.Remove(result, Scope);
                throw;
            }
        }
        public ActionResult Apply(Application app)
        {
            if (ModelState.IsValid)
            {
                appManager.Add(app);

                return(RedirectToAction("CompletedApplication"));
            }

            else
            {
                return(View(app));
            }
        }
        public string Add(Application obj)
        {
            Response resp = new Response();

            try
            {
                _app.Add(obj);
            }
            catch (Exception e)
            {
                resp.Code    = 500;
                resp.Message = e.Message;
            }
            return(JsonHelper.Instance.Serialize(resp));
        }
Esempio n. 6
0
        public void AddRemoveAppTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                AppManager manager  = new AppManager(clientContext);
                var        appBytes = OfficeDevPnP.Core.Tests.Properties.Resources.alm;

                var results = manager.Add(appBytes, $"app-{appGuid}.sppkg", true);

                Assert.IsNotNull(results);

                var removeResults = manager.Remove(results.Id);

                Assert.IsTrue(removeResults);
            }
        }
Esempio n. 7
0
        protected override void ExecuteCmdlet()
        {
            if (!System.IO.Path.IsPathRooted(Path))
            {
                Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
            }

            var fileInfo = new System.IO.FileInfo(Path);

            var bytes = System.IO.File.ReadAllBytes(Path);

            var manager = new AppManager(ClientContext);

            var result = manager.Add(bytes, fileInfo.Name);

            WriteObject(result);
        }
Esempio n. 8
0
        public void DeployRetractAppTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                AppManager manager  = new AppManager(clientContext);
                var        appBytes = OfficeDevPnP.Core.Tests.Properties.Resources.almskip;

                var results = manager.Add(appBytes, $"appalmskip-{appGuid}.sppkg", true);

                var deployResults = manager.Deploy(results.Id);
                Assert.IsTrue(deployResults);

                var metadata = manager.GetAvailable(results.Id);
                Assert.IsTrue(metadata.Deployed);

                var retractResults = manager.Retract(results.Id);
                Assert.IsTrue(retractResults);

                metadata = manager.GetAvailable(results.Id);
                Assert.IsFalse(metadata.Deployed);

                manager.Remove(results.Id);
            }
        }
Esempio n. 9
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (template.Tenant != null)
                {
                    var manager = new AppManager(web.Context as ClientContext);

                    if (template.Tenant.AppCatalog != null && template.Tenant.AppCatalog.Packages.Count > 0)
                    {
                        var appCatalogUri = web.GetAppCatalog();
                        if (appCatalogUri != null)
                        {
                            foreach (var app in template.Tenant.AppCatalog.Packages)
                            {
                                AppMetadata appMetadata = null;

                                if (app.Action == PackageAction.Upload ||
                                    app.Action == PackageAction.UploadAndPublish)
                                {
                                    using (var packageStream = GetPackageStream(template, app))
                                    {
                                        var memStream = new MemoryStream();
                                        packageStream.CopyTo(memStream);
                                        memStream.Position = 0;

                                        var appFilename = app.Src.Substring(app.Src.LastIndexOf('\\') + 1);
                                        appMetadata = manager.Add(memStream.ToArray(),
                                                                  appFilename,
                                                                  app.Overwrite);

                                        parser.Tokens.Add(new AppPackageIdToken(web, appFilename, appMetadata.Id));
                                    }
                                }

                                if (app.Action == PackageAction.Publish || app.Action == PackageAction.UploadAndPublish)
                                {
                                    if (appMetadata == null)
                                    {
                                        appMetadata = manager.GetAvailable()
                                                      .FirstOrDefault(a => a.Id == Guid.Parse(parser.ParseString(app.PackageId)));
                                    }
                                    if (appMetadata != null)
                                    {
                                        manager.Deploy(appMetadata, app.SkipFeatureDeployment);
                                    }
                                    else
                                    {
                                        scope.LogError("Referenced App Package {0} not available", app.PackageId);
                                        throw new Exception($"Referenced App Package {app.PackageId} not available");
                                    }
                                }

                                if (app.Action == PackageAction.Remove)
                                {
                                    var appId = Guid.Parse(parser.ParseString(app.PackageId));

                                    // Get the apps already installed in the site
                                    var appExists = manager.GetAvailable()?.Any(a => a.Id == appId);

                                    if (appExists.HasValue && appExists.Value)
                                    {
                                        manager.Remove(appId);
                                    }
                                    else
                                    {
                                        WriteMessage($"App Package with ID {appId} does not exist in the AppCatalog and cannot be removed!", ProvisioningMessageType.Warning);
                                    }
                                }
                            }
                        }
                        else
                        {
                            WriteMessage($"Tenant app catalog doesn't exist. ALM step will be skipped!", ProvisioningMessageType.Warning);
                        }
                    }

                    // So far we do not provision CDN settings
                    // It will come in the near future
                    // NOOP on CDN
                }
            }

            return(parser);
        }
Esempio n. 10
0
        public static TokenParser ProcessApps(Tenant tenant, ProvisioningTenant provisioningTenant, FileConnectorBase connector, TokenParser parser, PnPMonitoredScope scope, ProvisioningTemplateApplyingInformation applyingInformation, ProvisioningMessagesDelegate messagesDelegate)
        {
            if (provisioningTenant.AppCatalog != null && provisioningTenant.AppCatalog.Packages.Count > 0)
            {
                var rootSiteUrl = tenant.GetRootSiteUrl();
                tenant.Context.ExecuteQueryRetry();
                using (var context = ((ClientContext)tenant.Context).Clone(rootSiteUrl.Value, applyingInformation.AccessTokens))
                {
                    var web = context.Web;

                    Uri appCatalogUri = null;

                    try
                    {
                        appCatalogUri = web.GetAppCatalog();
                    }
                    catch (System.Net.WebException ex)
                    {
                        if (ex.Response != null)
                        {
                            var httpResponse = ex.Response as System.Net.HttpWebResponse;
                            if (httpResponse != null && httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                            {
                                // Ignore any security exception and simply keep
                                // the AppCatalog URI null
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                        else
                        {
                            throw ex;
                        }
                    }

                    if (appCatalogUri != null)
                    {
                        var manager = new AppManager(context);

                        foreach (var app in provisioningTenant.AppCatalog.Packages)
                        {
                            AppMetadata appMetadata = null;

                            if (app.Action == PackageAction.Upload || app.Action == PackageAction.UploadAndPublish)
                            {
                                var appSrc   = parser.ParseString(app.Src);
                                var appBytes = ConnectorFileHelper.GetFileBytes(connector, appSrc);

                                var hash = string.Empty;
                                using (var memoryStream = new MemoryStream(appBytes))
                                {
                                    hash = CalculateHash(memoryStream);
                                }

                                var exists = false;
                                var appId  = Guid.Empty;

                                using (var appCatalogContext = ((ClientContext)tenant.Context).Clone(appCatalogUri, applyingInformation.AccessTokens))
                                {
                                    // check if the app already is present
                                    var appList   = appCatalogContext.Web.GetListByUrl("AppCatalog");
                                    var camlQuery = new CamlQuery
                                    {
                                        ViewXml = string.Format(appExistsQuery, hash)
                                    };
                                    var items = appList.GetItems(camlQuery);
                                    appCatalogContext.Load(items, i => i.IncludeWithDefaultProperties());
                                    appCatalogContext.ExecuteQueryRetry();
                                    if (items.Count > 0)
                                    {
                                        exists = true;
                                        appId  = Guid.Parse(items[0].FieldValues["UniqueId"].ToString());
                                    }
                                }
                                var appFilename = appSrc.Substring(appSrc.LastIndexOf('\\') + 1);

                                if (!exists)
                                {
                                    messagesDelegate?.Invoke($"Processing solution {app.Src}", ProvisioningMessageType.Progress);
                                    appMetadata = manager.Add(appBytes, appFilename, app.Overwrite, timeoutSeconds: 500);
                                }
                                else
                                {
                                    messagesDelegate?.Invoke($"Skipping existing solution {app.Src}", ProvisioningMessageType.Progress);
                                    appMetadata = manager.GetAvailable().FirstOrDefault(a => a.Id == appId);
                                }
                                if (appMetadata != null)
                                {
                                    parser.AddToken(new AppPackageIdToken(web, appFilename, appMetadata.Id));
                                    parser.AddToken(new AppPackageIdToken(web, appMetadata.Title, appMetadata.Id));
                                }
                            }

                            if (app.Action == PackageAction.Publish || app.Action == PackageAction.UploadAndPublish)
                            {
                                if (appMetadata == null)
                                {
                                    appMetadata = manager.GetAvailable()
                                                  .FirstOrDefault(a => a.Id == Guid.Parse(parser.ParseString(app.PackageId)));
                                }
                                if (appMetadata != null)
                                {
                                    manager.Deploy(appMetadata, app.SkipFeatureDeployment);
                                }
                                else
                                {
                                    scope.LogError("Referenced App Package {0} not available", app.PackageId);
                                    throw new Exception($"Referenced App Package {app.PackageId} not available");
                                }
                            }

                            if (app.Action == PackageAction.Remove)
                            {
                                var appId = Guid.Parse(parser.ParseString(app.PackageId));

                                // Get the apps already installed in the site
                                var appExists = manager.GetAvailable()?.Any(a => a.Id == appId);

                                if (appExists.HasValue && appExists.Value)
                                {
                                    manager.Remove(appId);
                                }
                                else
                                {
                                    messagesDelegate?.Invoke($"App Package with ID {appId} does not exist in the AppCatalog and cannot be removed!", ProvisioningMessageType.Warning);
                                }
                            }
                        }
                    }
                    else
                    {
                        messagesDelegate?.Invoke($"Tenant app catalog doesn't exist. ALM step will be skipped!", ProvisioningMessageType.Warning);
                    }
                }
            }
            return(parser);
        }
Esempio n. 11
0
        private void ProcessApps(Web web, ProvisioningTemplate template, TokenParser parser, PnPMonitoredScope scope)
        {
            if (template.Tenant.AppCatalog != null && template.Tenant.AppCatalog.Packages.Count > 0)
            {
                var manager = new AppManager(web.Context as ClientContext);

                var appCatalogUri = web.GetAppCatalog();
                if (appCatalogUri != null)
                {
                    foreach (var app in template.Tenant.AppCatalog.Packages)
                    {
                        AppMetadata appMetadata = null;

                        if (app.Action == PackageAction.Upload || app.Action == PackageAction.UploadAndPublish)
                        {
                            var appBytes = GetFileBytes(template, app.Src);

                            var appFilename = app.Src.Substring(app.Src.LastIndexOf('\\') + 1);
                            appMetadata = manager.Add(appBytes, appFilename, app.Overwrite);

                            parser.Tokens.Add(new AppPackageIdToken(web, appFilename, appMetadata.Id));
                        }

                        if (app.Action == PackageAction.Publish || app.Action == PackageAction.UploadAndPublish)
                        {
                            if (appMetadata == null)
                            {
                                appMetadata = manager.GetAvailable()
                                              .FirstOrDefault(a => a.Id == Guid.Parse(parser.ParseString(app.PackageId)));
                            }
                            if (appMetadata != null)
                            {
                                manager.Deploy(appMetadata, app.SkipFeatureDeployment);
                            }
                            else
                            {
                                scope.LogError("Referenced App Package {0} not available", app.PackageId);
                                throw new Exception($"Referenced App Package {app.PackageId} not available");
                            }
                        }

                        if (app.Action == PackageAction.Remove)
                        {
                            var appId = Guid.Parse(parser.ParseString(app.PackageId));

                            // Get the apps already installed in the site
                            var appExists = manager.GetAvailable()?.Any(a => a.Id == appId);

                            if (appExists.HasValue && appExists.Value)
                            {
                                manager.Remove(appId);
                            }
                            else
                            {
                                WriteMessage($"App Package with ID {appId} does not exist in the AppCatalog and cannot be removed!", ProvisioningMessageType.Warning);
                            }
                        }
                    }
                }
                else
                {
                    WriteMessage($"Tenant app catalog doesn't exist. ALM step will be skipped!", ProvisioningMessageType.Warning);
                }
            }
        }