protected override void ExecuteCmdlet()
        {
            // If no URL specified, we take the URL of the site that the current context is connected to
            if (!ParameterSpecified(nameof(Url)))
            {
                Url = PnPConnection.Current.Url;
            }

            // Generate site script
            var tenantSiteScriptSerializationInfo = new TenantSiteScriptSerializationInfo
            {
                IncludeBranding                      = IncludeBranding || IncludeAll,
                IncludedLists                        = Lists?.Select(l => l.Replace("\\", "/")).ToArray(),
                IncludeLinksToExportedItems          = IncludeLinksToExportedItems || IncludeAll,
                IncludeRegionalSettings              = IncludeRegionalSettings || IncludeAll,
                IncludeSiteExternalSharingCapability = IncludeSiteExternalSharingCapability || IncludeAll,
                IncludeTheme = IncludeTheme || IncludeAll
            };
            var generatedSiteScript = Tenant.GetSiteScriptFromSite(Url, tenantSiteScriptSerializationInfo);

            ClientContext.ExecuteQueryRetry();

            var siteScript = generatedSiteScript.Value.JSON;

            // Add the site script as a new site script to the tenant
            TenantSiteScriptCreationInfo siteScriptCreationInfo = new TenantSiteScriptCreationInfo
            {
                Title       = Title,
                Description = Description,
                Content     = siteScript
            };

            var addedSiteScript = Tenant.CreateSiteScript(siteScriptCreationInfo);

            ClientContext.Load(addedSiteScript);
            ClientContext.ExecuteQueryRetry();

            // Create a site design
            TenantSiteDesignCreationInfo siteDesignInfo = new TenantSiteDesignCreationInfo
            {
                Title               = Title,
                SiteScriptIds       = new [] { addedSiteScript.Id },
                Description         = Description,
                IsDefault           = IsDefault,
                PreviewImageAltText = PreviewImageAltText,
                PreviewImageUrl     = PreviewImageUrl,
                WebTemplate         = ((int)WebTemplate).ToString(),
                ThumbnailUrl        = ThumbnailUrl,
                DesignPackageId     = DesignPackageId
            };

            var design = Tenant.CreateSiteDesign(siteDesignInfo);

            ClientContext.Load(design);
            ClientContext.ExecuteQueryRetry();
            WriteObject(design);
        }
Example #2
0
        //gavdcodeend 36

        //gavdcodebegin 37
        static void SpCsCsomAddSiteDesign(ClientContext spCtx)
        {
            Guid myId = new Guid("79a5174f-0712-49c7-b6af-5a45918c55ee");

            Tenant myTenant = new Tenant(spCtx);

            TenantSiteDesignCreationInfo myInfo = new TenantSiteDesignCreationInfo()
            {
                Title         = "Custom List From Site Design CSOM",
                WebTemplate   = "64",
                SiteScriptIds = new Guid[] { myId },
                Description   = "Creates a Custom List in a site using CSOM Site Design"
            };

            var response = myTenant.CreateSiteDesign(myInfo);

            spCtx.ExecuteQuery();
        }
Example #3
0
        protected override void ExecuteCmdlet()
        {
            TenantSiteDesignCreationInfo siteDesignInfo = new TenantSiteDesignCreationInfo
            {
                Title               = Title,
                SiteScriptIds       = SiteScriptIds.Select(t => t.Id).ToArray(),
                Description         = Description,
                IsDefault           = IsDefault,
                PreviewImageAltText = PreviewImageAltText,
                PreviewImageUrl     = PreviewImageUrl,
                WebTemplate         = ((int)WebTemplate).ToString()
            };

            var design = Tenant.CreateSiteDesign(siteDesignInfo);

            ClientContext.Load(design);
            ClientContext.ExecuteQueryRetry();
            WriteObject(design);
        }
Example #4
0
        public static TokenParser ProcessSiteDesigns(Tenant tenant, ProvisioningTenant provisioningTenant, TokenParser parser, PnPMonitoredScope scope, ProvisioningMessagesDelegate messagesDelegate)
        {
            if (provisioningTenant.SiteDesigns != null && provisioningTenant.SiteDesigns.Any())
            {
                var existingDesigns = tenant.GetSiteDesigns();
                tenant.Context.Load(existingDesigns);
                tenant.Context.ExecuteQueryRetry();
                foreach (var siteDesign in provisioningTenant.SiteDesigns)
                {
                    var parsedTitle               = parser.ParseString(siteDesign.Title);
                    var parsedDescription         = parser.ParseString(siteDesign.Description);
                    var parsedPreviewImageUrl     = parser.ParseString(siteDesign.PreviewImageUrl);
                    var parsedPreviewImageAltText = parser.ParseString(siteDesign.PreviewImageAltText);
                    messagesDelegate?.Invoke($"Processing site design {parsedTitle}", ProvisioningMessageType.Progress);

                    var existingSiteDesign = existingDesigns.FirstOrDefault(d => d.Title == parsedTitle);
                    if (existingSiteDesign == null)
                    {
                        TenantSiteDesignCreationInfo siteDesignCreationInfo = new TenantSiteDesignCreationInfo()
                        {
                            Title               = parsedTitle,
                            Description         = parsedDescription,
                            PreviewImageUrl     = parsedPreviewImageUrl,
                            PreviewImageAltText = parsedPreviewImageAltText,
                            IsDefault           = siteDesign.IsDefault,
                        };
                        switch ((int)siteDesign.WebTemplate)
                        {
                        case 0:
                        {
                            siteDesignCreationInfo.WebTemplate = "64";
                            break;
                        }

                        case 1:
                        {
                            siteDesignCreationInfo.WebTemplate = "68";
                            break;
                        }
                        }
                        if (siteDesign.SiteScripts != null && siteDesign.SiteScripts.Any())
                        {
                            List <Guid> ids = new List <Guid>();
                            foreach (var siteScriptRef in siteDesign.SiteScripts)
                            {
                                ids.Add(Guid.Parse(parser.ParseString(siteScriptRef)));
                            }
                            siteDesignCreationInfo.SiteScriptIds = ids.ToArray();
                        }
                        var design = tenant.CreateSiteDesign(siteDesignCreationInfo);
                        tenant.Context.Load(design);
                        tenant.Context.ExecuteQueryRetry();

                        if (siteDesign.Grants != null && siteDesign.Grants.Any())
                        {
                            foreach (var grant in siteDesign.Grants)
                            {
                                var rights = (TenantSiteDesignPrincipalRights)Enum.Parse(typeof(TenantSiteDesignPrincipalRights), grant.Right.ToString());
                                tenant.GrantSiteDesignRights(design.Id, new[] { grant.Principal }, rights);
                            }
                            tenant.Context.ExecuteQueryRetry();
                        }
                        parser.AddToken(new SiteDesignIdToken(null, design.Title, design.Id));
                    }
                    else
                    {
                        if (siteDesign.Overwrite)
                        {
                            var existingId = existingSiteDesign.Id;
                            existingSiteDesign = Tenant.GetSiteDesign(tenant.Context, existingId);
                            tenant.Context.ExecuteQueryRetry();

                            existingSiteDesign.Title               = parsedTitle;
                            existingSiteDesign.Description         = parsedDescription;
                            existingSiteDesign.PreviewImageUrl     = parsedPreviewImageUrl;
                            existingSiteDesign.PreviewImageAltText = parsedPreviewImageAltText;
                            existingSiteDesign.IsDefault           = siteDesign.IsDefault;
                            switch ((int)siteDesign.WebTemplate)
                            {
                            case 0:
                            {
                                existingSiteDesign.WebTemplate = "64";
                                break;
                            }

                            case 1:
                            {
                                existingSiteDesign.WebTemplate = "68";
                                break;
                            }
                            }

                            tenant.UpdateSiteDesign(existingSiteDesign);
                            tenant.Context.ExecuteQueryRetry();

                            var existingToken = parser.Tokens.OfType <SiteDesignIdToken>().FirstOrDefault(t => t.GetReplaceValue() == existingId.ToString());
                            if (existingToken != null)
                            {
                                parser.Tokens.Remove(existingToken);
                            }
                            parser.AddToken(new SiteScriptIdToken(null, parsedTitle, existingId));

                            if (siteDesign.Grants != null && siteDesign.Grants.Any())
                            {
                                var existingRights = Tenant.GetSiteDesignRights(tenant.Context, existingId);
                                tenant.Context.Load(existingRights);
                                tenant.Context.ExecuteQueryRetry();
                                foreach (var existingRight in existingRights)
                                {
                                    Tenant.RevokeSiteDesignRights(tenant.Context, existingId, new[] { existingRight.PrincipalName });
                                }
                                foreach (var grant in siteDesign.Grants)
                                {
                                    var rights = (TenantSiteDesignPrincipalRights)Enum.Parse(typeof(TenantSiteDesignPrincipalRights), grant.Right.ToString());
                                    tenant.GrantSiteDesignRights(existingId, new[] { parser.ParseString(grant.Principal) }, rights);
                                }
                                tenant.Context.ExecuteQueryRetry();
                            }
                        }
                    }
                }
            }
            return(parser);
        }
Example #5
0
        private TokenParser ProcessSiteDesigns(Web web, ProvisioningTemplate template, TokenParser parser, PnPMonitoredScope scope)
        {
            if (template.Tenant.SiteDesigns != null && template.Tenant.SiteDesigns.Any())
            {
                using (var tenantContext = web.Context.Clone(web.GetTenantAdministrationUrl()))
                {
                    var tenant = new Tenant(tenantContext);

                    var existingDesigns = tenant.GetSiteDesigns();
                    tenantContext.Load(existingDesigns);
                    tenantContext.ExecuteQueryRetry();
                    foreach (var siteDesign in template.Tenant.SiteDesigns)
                    {
                        var designTitle               = parser.ParseString(siteDesign.Title);
                        var designDescription         = parser.ParseString(siteDesign.Description);
                        var designPreviewImageUrl     = parser.ParseString(siteDesign.PreviewImageUrl);
                        var designPreviewImageAltText = parser.ParseString(siteDesign.PreviewImageAltText);

                        var existingSiteDesign = existingDesigns.FirstOrDefault(d => d.Title == designTitle);
                        if (existingSiteDesign == null)
                        {
                            TenantSiteDesignCreationInfo siteDesignCreationInfo = new TenantSiteDesignCreationInfo()
                            {
                                Title               = designTitle,
                                Description         = designDescription,
                                PreviewImageUrl     = designPreviewImageUrl,
                                PreviewImageAltText = designPreviewImageAltText,
                                IsDefault           = siteDesign.IsDefault,
                                WebTemplate         = ((int)siteDesign.WebTemplate).ToString() // convert TeamSite to 64, CommunicationSite to 68
                            };
                            if (siteDesign.SiteScripts != null && siteDesign.SiteScripts.Any())
                            {
                                List <Guid> ids = new List <Guid>();
                                foreach (var siteScriptRef in siteDesign.SiteScripts)
                                {
                                    ids.Add(Guid.Parse(parser.ParseString(siteScriptRef)));
                                }
                                siteDesignCreationInfo.SiteScriptIds = ids.ToArray();
                            }
                            var design = tenant.CreateSiteDesign(siteDesignCreationInfo);
                            tenantContext.Load(design);
                            tenantContext.ExecuteQueryRetry();

                            if (siteDesign.Grants != null && siteDesign.Grants.Any())
                            {
                                foreach (var grant in siteDesign.Grants)
                                {
                                    var rights = (TenantSiteDesignPrincipalRights)Enum.Parse(typeof(TenantSiteDesignPrincipalRights), grant.Right.ToString());
                                    tenant.GrantSiteDesignRights(design.Id, new[] { grant.Principal }, rights);
                                }
                                tenantContext.ExecuteQueryRetry();
                            }
                            parser.AddToken(new SiteDesignIdToken(web, design.Title, design.Id));
                        }
                        else
                        {
                            if (siteDesign.Overwrite)
                            {
                                var existingId = existingSiteDesign.Id;
                                existingSiteDesign = Tenant.GetSiteDesign(tenantContext, existingId);
                                tenantContext.ExecuteQueryRetry();

                                existingSiteDesign.Title               = designTitle;
                                existingSiteDesign.Description         = designDescription;
                                existingSiteDesign.PreviewImageUrl     = designPreviewImageUrl;
                                existingSiteDesign.PreviewImageAltText = designPreviewImageAltText;
                                existingSiteDesign.IsDefault           = siteDesign.IsDefault;
                                existingSiteDesign.WebTemplate         = ((int)siteDesign.WebTemplate).ToString(); // convert TeamSite to 64, CommunicationSite to 68

                                tenant.UpdateSiteDesign(existingSiteDesign);
                                tenantContext.ExecuteQueryRetry();

                                var existingToken = parser.Tokens.OfType <SiteDesignIdToken>().FirstOrDefault(t => t.GetReplaceValue() == existingId.ToString());
                                if (existingToken != null)
                                {
                                    parser.Tokens.Remove(existingToken);
                                }
                                parser.AddToken(new SiteScriptIdToken(web, designTitle, existingId));

                                if (siteDesign.Grants != null && siteDesign.Grants.Any())
                                {
                                    var existingRights = Tenant.GetSiteDesignRights(tenantContext, existingId);
                                    tenantContext.Load(existingRights);
                                    tenantContext.ExecuteQueryRetry();
                                    foreach (var existingRight in existingRights)
                                    {
                                        Tenant.RevokeSiteDesignRights(tenantContext, existingId, new[] { existingRight.PrincipalName });
                                    }
                                    foreach (var grant in siteDesign.Grants)
                                    {
                                        var rights = (TenantSiteDesignPrincipalRights)Enum.Parse(typeof(TenantSiteDesignPrincipalRights), grant.Right.ToString());
                                        tenant.GrantSiteDesignRights(existingId, new[] { parser.ParseString(grant.Principal) }, rights);
                                    }
                                    tenantContext.ExecuteQueryRetry();
                                }
                            }
                        }
                    }
                }
            }
            return(parser);
        }