public string CreateSiteCollection()
        {
            var user   = GetSpUser.ResolveUserById(_ctx, _actionRequest.User);
            var tenant = new Tenant(_ctx);

            var webUrl =
                $"https://{_actionRequest.TenantName}.sharepoint.com/{_siteCollectionRequest.ManagedPath}/{_actionRequest.Name}";

            if (tenant.SiteExists(webUrl))
            {
                // "Site already existed. Used URL - {0}"
            }
            else
            {
                var newsite = new SiteCreationProperties
                {
                    Title                = _actionRequest.Name,
                    Url                  = webUrl,
                    Owner                = user.Email,
                    Template             = "STS#0",
                    Lcid                 = _siteCollectionRequest.Lcid,
                    TimeZoneId           = _siteCollectionRequest.TimeZoneId,
                    StorageMaximumLevel  = _siteCollectionRequest.StorageMaximumLevel,
                    StorageWarningLevel  = _siteCollectionRequest.StorageMaximumLevel,
                    UserCodeMaximumLevel = 0,
                    UserCodeWarningLevel = 0
                };

                SpoOperation op = tenant.CreateSite(newsite);
                _ctx.Load(tenant);
                _ctx.Load(op, i => i.IsComplete);
                _ctx.ExecuteQuery();

                var step = 0;
                while (!op.IsComplete)
                {
                    step = step + 1;
                    Updatehelper.UpdateProgressView($"{step} Creating site collection", _actionRequest);
                    Thread.Sleep(10000);
                    op.RefreshLoad();
                    _ctx.ExecuteQuery();
                }
            }
            var site = tenant.GetSiteByUrl(webUrl);
            var web  = site.RootWeb;

            web.Description = _actionRequest.Description;
            web.Update();
            _ctx.Load(web);
            _ctx.ExecuteQuery();

            return(web.Url);
        }
        public void ApplyCustomTemplateToSite()
        {
            var provisioningTemplate = _siteTemplate.PnpTemplate;
            var applyingInfo         = new ProvisioningTemplateApplyingInformation
            {
                ProgressDelegate =
                    (message, step, total) =>
                {
                    Updatehelper.UpdateProgressView($"{step}/{total} Provisioning {message}", _actionRequest);
                }
            };

            _ctx.Web.ApplyProvisioningTemplate(provisioningTemplate, applyingInfo);
        }
Exemplo n.º 3
0
        public void CreateSite()
        {
            Updatehelper.UpdateProgressView("Creating site", _actionRequest);

            var newWeb = _ctx.Web.CreateWeb(
                new OfficeDevPnP.Core.Entities.SiteEntity()
            {
                Title       = _actionRequest.Name,
                Url         = _actionRequest.Name,
                Description = _actionRequest.Description,
                Template    = "STS#0"
            });

            Updatehelper.UpdateProgressView("Created", _actionRequest);
        }
Exemplo n.º 4
0
        public static void ProcessQueueMessage([QueueTrigger("siteprovisioningqueue")] ActionRequest actionRequest, TextWriter log)
        {
            try
            {
                //   Updatehelper.UpdateDashboard("Request receiveded.");

                Log.AddlogToTable(actionRequest, State.Provisioning);

                if (actionRequest.IsSiteCollection)
                {
                    CreateSiteCollection(actionRequest);
                    Updatehelper.UpdateProgressView("Provisioning", actionRequest);
                }
                else
                {
                    CreateSite(actionRequest);
                    Updatehelper.UpdateProgressView("Create site", actionRequest);
                }

                Log.AddlogToTable(actionRequest, State.Created);
                Console.WriteLine(actionRequest.Url + "/" + actionRequest.Name, actionRequest.Name, actionRequest.User);
                //var mail = new NotificationMail();
                //mail.SendMailNotification(actionRequest.Url + "/" + actionRequest.Name, actionRequest.Name, actionRequest.User);
                Updatehelper.UpdateProgressView("Done", actionRequest);
            }
            catch (Exception ex)
            {
                Log.AddlogToTable(actionRequest, State.Failed);
                Updatehelper.UpdateProgressView("Failed", actionRequest);

                Console.WriteLine(ex.ToString(), "Failed creating site: " + actionRequest.Url + "/" + actionRequest.Name,
                                  actionRequest.User);

                //var mail = new NotificationMail();
                //mail.SendMailNotification(ex.ToString(), "Failed creating site: " + actionRequest.Url + "/" + actionRequest.Name, actionRequest.User);

                if (actionRequest.IsSiteCollection)
                {
                    var tenantAdminUri = new Uri($"https://{actionRequest.TenantName}-admin.sharepoint.com");
                    var realm          = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
                    var token          =
                        TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, tenantAdminUri.Authority,
                                                          realm).AccessToken;
                    using (var ctx = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
                    {
                        var manager = new SiteCollectionManager(actionRequest, ctx);
                        manager.TryDeleteSiteCollection();
                    }
                }
                else
                {
                    var tenantAdminUri = new Uri(actionRequest.Url);
                    var realm          = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
                    var token          =
                        TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, tenantAdminUri.Authority,
                                                          realm).AccessToken;
                    using (var ctx = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
                    {
                        var manager = new SiteManager(actionRequest, ctx);
                        manager.TryDeleteSite();
                    }
                }
            }
        }