Ejemplo n.º 1
0
        protected override void ExecuteCmdlet()
        {
            if (Type == SiteType.CommunicationSite)
            {
                if (!ParameterSpecified("Lcid"))
                {
                    ClientContext.Web.EnsureProperty(w => w.Language);
                    _communicationSiteParameters.Lcid = ClientContext.Web.Language;
                }
                var creationInformation = new PnP.Framework.Sites.CommunicationSiteCollectionCreationInformation();
                creationInformation.Title          = _communicationSiteParameters.Title;
                creationInformation.Url            = _communicationSiteParameters.Url;
                creationInformation.Description    = _communicationSiteParameters.Description;
                creationInformation.Classification = _communicationSiteParameters.Classification;
#pragma warning disable CS0618 // Type or member is obsolete
                creationInformation.ShareByEmailEnabled = _communicationSiteParameters.ShareByEmailEnabled;
#pragma warning restore CS0618 // Type or member is obsolete
                creationInformation.Lcid = _communicationSiteParameters.Lcid;
                if (ParameterSpecified(nameof(HubSiteId)))
                {
                    creationInformation.HubSiteId = HubSiteId;
                }
                if (ParameterSetName == ParameterSet_COMMUNICATIONCUSTOMDESIGN)
                {
                    creationInformation.SiteDesignId = _communicationSiteParameters.SiteDesignId;
                }
                else
                {
                    creationInformation.SiteDesign = _communicationSiteParameters.SiteDesign;
                }
                creationInformation.Owner = _communicationSiteParameters.Owner;
                creationInformation.PreferredDataLocation = _communicationSiteParameters.PreferredDataLocation;
                creationInformation.SensitivityLabel      = _communicationSiteParameters.SensitivityLabel;

                var returnedContext = PnP.Framework.Sites.SiteCollection.Create(ClientContext, creationInformation, noWait: !Wait);
                WriteObject(returnedContext.Url);
            }
            else
            {
                var creationInformation = new PnP.Framework.Sites.TeamSiteCollectionCreationInformation();
                creationInformation.DisplayName    = _teamSiteParameters.Title;
                creationInformation.Alias          = _teamSiteParameters.Alias;
                creationInformation.Classification = _teamSiteParameters.Classification;
                creationInformation.Description    = _teamSiteParameters.Description;
                creationInformation.IsPublic       = _teamSiteParameters.IsPublic;
                creationInformation.Lcid           = _teamSiteParameters.Lcid;
                if (ParameterSpecified(nameof(HubSiteId)))
                {
                    creationInformation.HubSiteId = HubSiteId;
                }
                creationInformation.Owners = _teamSiteParameters.Owners;
                creationInformation.PreferredDataLocation = _teamSiteParameters.PreferredDataLocation;
                creationInformation.SensitivityLabel      = _teamSiteParameters.SensitivityLabel;

                var returnedContext = PnP.Framework.Sites.SiteCollection.Create(ClientContext, creationInformation, noWait: !Wait, graphAccessToken: GraphAccessToken);
                WriteObject(returnedContext.Url);
            }
        }
        private async Task <string> CreateCommunicationSite(ClientContext clientContext, string newSiteTitle, bool allowScripts = false)
        {
            var communicationSiteGuid = Guid.NewGuid().ToString("N");
            var baseUri        = new Uri(clientContext.Url);
            var baseUrl        = $"{baseUri.Scheme}://{baseUri.Host}:{baseUri.Port}";
            var newCommSiteUrl = $"{baseUrl}/sites/site{communicationSiteGuid}";

            // create new site to apply template to
            var siteCollectionCreationInformation = new PnP.Framework.Sites.CommunicationSiteCollectionCreationInformation()
            {
                Url        = $"{baseUrl}/sites/site{communicationSiteGuid}",
                SiteDesign = PnP.Framework.Sites.CommunicationSiteDesign.Blank,
                Title      = newSiteTitle,
                Lcid       = 1033
            };

            // TODO: Owner set the owner

            /*
             * if (clientContext.IsAppOnly()
             *  && string.IsNullOrEmpty(siteCollectionCreationInformation.Owner)
             *  && !string.IsNullOrEmpty(TestCommon.DefaultSiteOwner))
             * {
             *  siteCollectionCreationInformation.Owner = TestCommon.DefaultSiteOwner;
             * }
             */
            var commResults = await clientContext.CreateSiteAsync(siteCollectionCreationInformation);

            Assert.IsNotNull(commResults);

            if (allowScripts)
            {
                AllowScripting(newCommSiteUrl, true);
            }

            return(newCommSiteUrl);
        }
Ejemplo n.º 3
0
        protected override void ExecuteCmdlet()
        {
            if (Type == SiteType.CommunicationSite)
            {
                if (!ParameterSpecified("Lcid"))
                {
                    ClientContext.Web.EnsureProperty(w => w.Language);
                    _communicationSiteParameters.Lcid = ClientContext.Web.Language;
                }
                var creationInformation = new PnP.Framework.Sites.CommunicationSiteCollectionCreationInformation();
                creationInformation.Title          = _communicationSiteParameters.Title;
                creationInformation.Url            = _communicationSiteParameters.Url;
                creationInformation.Description    = _communicationSiteParameters.Description;
                creationInformation.Classification = _communicationSiteParameters.Classification;
#pragma warning disable CS0618 // Type or member is obsolete
                creationInformation.ShareByEmailEnabled = _communicationSiteParameters.ShareByEmailEnabled;
#pragma warning restore CS0618 // Type or member is obsolete
                creationInformation.Lcid = _communicationSiteParameters.Lcid;
                if (ParameterSpecified(nameof(HubSiteId)))
                {
                    creationInformation.HubSiteId = HubSiteId;
                }
                if (ParameterSetName == ParameterSet_COMMUNICATIONCUSTOMDESIGN)
                {
                    creationInformation.SiteDesignId = _communicationSiteParameters.SiteDesignId;
                }
                else
                {
                    creationInformation.SiteDesign = _communicationSiteParameters.SiteDesign;
                }
                creationInformation.Owner = _communicationSiteParameters.Owner;
                creationInformation.PreferredDataLocation = _communicationSiteParameters.PreferredDataLocation;
                creationInformation.SensitivityLabel      = _communicationSiteParameters.SensitivityLabel;

                var returnedContext = PnP.Framework.Sites.SiteCollection.Create(ClientContext, creationInformation, noWait: !Wait);
                WriteObject(returnedContext.Url);
            }
            else
            {
                var creationInformation = new PnP.Framework.Sites.TeamSiteCollectionCreationInformation();
                creationInformation.DisplayName    = _teamSiteParameters.Title;
                creationInformation.Alias          = _teamSiteParameters.Alias;
                creationInformation.Classification = _teamSiteParameters.Classification;
                creationInformation.Description    = _teamSiteParameters.Description;
                creationInformation.IsPublic       = _teamSiteParameters.IsPublic;
                creationInformation.Lcid           = _teamSiteParameters.Lcid;
                if (ParameterSpecified(nameof(HubSiteId)))
                {
                    creationInformation.HubSiteId = HubSiteId;
                }
                creationInformation.Owners = _teamSiteParameters.Owners;
                creationInformation.PreferredDataLocation = _teamSiteParameters.PreferredDataLocation;
                creationInformation.SensitivityLabel      = _teamSiteParameters.SensitivityLabel;

                if (ClientContext.GetContextSettings()?.Type != Framework.Utilities.Context.ClientContextType.SharePointACSAppOnly)
                {
                    var returnedContext = PnP.Framework.Sites.SiteCollection.Create(ClientContext, creationInformation, noWait: !Wait, graphAccessToken: GraphAccessToken);
                    WriteObject(returnedContext.Url);
                }
                else
                {
                    WriteError(new PSInvalidOperationException("Creating a new teamsite requires an underlying Microsoft 365 group. In order to create this we need to acquire an access token for the Microsoft Graph. This is not possible using ACS App Only connections."), ErrorCategory.SecurityError);
                }
            }
        }
Ejemplo n.º 4
0
        private void ProcessBaseTemplates(List <BaseTemplate> templates, bool deleteSites, bool createSites)
        {
            using (var tenantCtx = TestCommon.CreateTenantClientContext())
            {
                tenantCtx.RequestTimeout = 1000 * 60 * 15;
                Tenant tenant = new Tenant(tenantCtx);

                if (deleteSites)
                {
                    // First delete all template site collections when in SPO
                    foreach (var template in templates)
                    {
                        string siteUrl = GetSiteUrl(template);

                        try
                        {
                            Console.WriteLine("Deleting existing site {0}", siteUrl);
                            if (template.SkipDeleteCreateCycle)
                            {
                                // Do nothing for the time being since we don't allow group deletion using app-only context
                            }
                            else
                            {
                                tenant.DeleteSiteCollection(siteUrl, false);
                            }
                        }
                        catch { }
                    }
                }

                if (createSites)
                {
                    // Create site collections
                    foreach (var template in templates)
                    {
                        string siteUrl = GetSiteUrl(template);

                        Console.WriteLine("Creating site {0}", siteUrl);

                        if (template.SkipDeleteCreateCycle)
                        {
                            // Do nothing for the time being since we don't allow group creation using app-only context
                        }
                        else
                        {
                            bool siteExists = false;
                            if (template.SubSiteTemplate.Length > 0)
                            {
                                siteExists = tenant.SiteExists(siteUrl);
                            }

                            if (!siteExists)
                            {
                                if (template.Template.StartsWith("SITEPAGEPUBLISHING"))
                                {
                                    using (var clientContext = TestCommon.CreateClientContext())
                                    {
                                        var newCommunicationSiteCreationInfo = new PnP.Framework.Sites.CommunicationSiteCollectionCreationInformation()
                                        {
                                            Url        = siteUrl,
                                            SiteDesign = PnP.Framework.Sites.CommunicationSiteDesign.Blank,
                                            Title      = "Template Site",
                                            Lcid       = 1033
                                        };

                                        clientContext.CreateSiteAsync(newCommunicationSiteCreationInfo).Wait();
                                    }
                                }
                                else
                                {
                                    var newSiteEntity = new Entities.SiteEntity()
                                    {
                                        Lcid           = 1033,
                                        TimeZoneId     = 4,
                                        SiteOwnerLogin = TestCommon.UserName,
                                        Title          = "Template Site",
                                        Template       = template.Template,
                                        Url            = siteUrl,
                                    };

                                    tenant.CreateSiteCollection(newSiteEntity, true, true);
                                }
                            }

                            if (template.SubSiteTemplate.Length > 0)
                            {
                                using (ClientContext ctx = TestCommon.CreateClientContext())
                                {
                                    using (var sitecolCtx = ctx.Clone(siteUrl))
                                    {
                                        sitecolCtx.Web.Webs.Add(new WebCreationInformation()
                                        {
                                            Title    = string.Format("template{0}", template.SubSiteTemplate),
                                            Language = 1033,
                                            Url      = string.Format("template{0}", template.SubSiteTemplate.Replace("#", "")),
                                            UseSamePermissionsAsParentSite = true
                                        });
                                        sitecolCtx.ExecuteQueryRetry();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Export the base templates
            using (ClientContext ctx = TestCommon.CreateClientContext())
            {
                foreach (var template in templates)
                {
                    string siteUrl = GetSiteUrl(template, false);

                    // Export the base templates
                    using (ClientContext cc = ctx.Clone(siteUrl))
                    {
                        cc.RequestTimeout = 1000 * 60 * 15;

                        // Specify null as base template since we do want "everything" in this case
                        ProvisioningTemplateCreationInformation creationInfo = new ProvisioningTemplateCreationInformation(cc.Web);
                        creationInfo.BaseTemplate = null;
                        // Do not extract the home page for the base templates
                        creationInfo.HandlersToProcess ^= Handlers.PageContents;

                        // Override the save name. Case is online site collection provisioned using blankinternetcontainer#0 which returns
                        // blankinternet#0 as web template using CSOM/SSOM API
                        string templateName = template.Template;
                        if (template.SaveAsTemplate.Length > 0)
                        {
                            templateName = template.SaveAsTemplate;
                        }

                        ProvisioningTemplate p = cc.Web.GetProvisioningTemplate(creationInfo);
                        if (template.SubSiteTemplate.Length > 0)
                        {
                            p.Id = String.Format("{0}template", template.SubSiteTemplate.Replace("#", ""));
                        }
                        else
                        {
                            p.Id = String.Format("{0}template", templateName.Replace("#", ""));
                        }

                        // Cleanup before saving
                        p.Security.AdditionalAdministrators.Clear();

                        // persist the template using the XML provider
                        XMLFileSystemTemplateProvider provider = new XMLFileSystemTemplateProvider(".", "");
                        if (template.SubSiteTemplate.Length > 0)
                        {
                            provider.SaveAs(p, String.Format("{0}Template.xml", template.SubSiteTemplate.Replace("#", "")));
                        }
                        else
                        {
                            provider.SaveAs(p, String.Format("{0}Template.xml", templateName.Replace("#", "")));
                        }
                    }
                }
            }
        }