Exemple #1
0
        protected override void ExecuteCmdlet()
        {
            IPage clientSidePage = null;

            // Check if the page exists
            string name = PageUtilities.EnsureCorrectPageName(Name);

            bool pageExists = false;

            try
            {
                var pages = PnPContext.Web.GetPages(name);
                if (pages != null && pages.FirstOrDefault(p => p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) != null)
                {
                    pageExists = true;
                }
            }
            catch { }

            if (pageExists)
            {
                throw new Exception($"Page {name} already exists");
            }

            // Create a page that persists immediately
            clientSidePage = PnPContext.Web.NewPage(LayoutType);
            clientSidePage.PageHeader.LayoutType = HeaderLayoutType;

            if (PromoteAs == PagePromoteType.Template)
            {
                clientSidePage.SaveAsTemplate(name);
            }
            else
            {
                clientSidePage.Save(name);
            }

            if (ContentType != null)
            {
                string ctId     = ContentType.GetIdOrThrow(nameof(ContentType), CurrentWeb);
                var    pageFile = clientSidePage.GetPageFile(p => p.UniqueId, p => p.ListId, p => p.ListItemAllFields);
                pageFile.ListItemAllFields["ContentTypeId"] = ctId;
                pageFile.ListItemAllFields.SystemUpdate();
            }

            // If a specific promote type is specified, promote the page as Home or Article or ...
            switch (PromoteAs)
            {
            case PagePromoteType.HomePage:
                clientSidePage.PromoteAsHomePage();
                break;

            case PagePromoteType.NewsArticle:
                clientSidePage.PromoteAsNewsArticle();
                break;

            case PagePromoteType.None:
            default:
                break;
            }

            if (ParameterSpecified(nameof(CommentsEnabled)))
            {
                if (CommentsEnabled)
                {
                    clientSidePage.EnableComments();
                }
                else
                {
                    clientSidePage.DisableComments();
                }
            }

            if (Publish)
            {
                clientSidePage.Publish();
            }

            WriteObject(clientSidePage);
        }
Exemple #2
0
        protected override void ExecuteCmdlet()
        {
            var clientSidePage = Identity?.GetPage();

            if (clientSidePage == null)
            {
                // If the client side page object cannot be found
                throw new Exception($"Page {Identity?.Name} cannot be found.");
            }

            // We need to have the page name, if not found, raise an error
            string name = PageUtilities.EnsureCorrectPageName(Name ?? Identity?.Name);

            if (name == null)
            {
                throw new Exception("Insufficient arguments to update a client side page");
            }

            clientSidePage.LayoutType = LayoutType;

            if (Title != null)
            {
                clientSidePage.PageTitle = Title;
            }

            if (ThumbnailUrl != null)
            {
                clientSidePage.ThumbnailUrl = ThumbnailUrl;
            }

            if (ParameterSpecified(nameof(HeaderType)))
            {
                switch (HeaderType)
                {
                case PageHeaderType.Default:
                {
                    clientSidePage.SetDefaultPageHeader();
                    break;
                }

                case PageHeaderType.Custom:
                {
                    clientSidePage.SetCustomPageHeader(customHeaderParameters.ServerRelativeImageUrl, customHeaderParameters.TranslateX, customHeaderParameters.TranslateY);
                    break;
                }

                case PageHeaderType.None:
                {
                    clientSidePage.RemovePageHeader();
                    break;
                }
                }
            }

            if (ParameterSpecified(nameof(HeaderLayoutType)))
            {
                clientSidePage.PageHeader.LayoutType = HeaderLayoutType;
            }

            if (PromoteAs == PagePromoteType.Template)
            {
                clientSidePage.SaveAsTemplate(name);
            }
            else
            {
                clientSidePage.Save(name);
            }

            // If a specific promote type is specified, promote the page as Home or Article or ...
            switch (PromoteAs)
            {
            case PagePromoteType.HomePage:
                clientSidePage.PromoteAsHomePage();
                break;

            case PagePromoteType.NewsArticle:
                clientSidePage.PromoteAsNewsArticle();
                break;

            case PagePromoteType.None:
            default:
                break;
            }

            if (ParameterSpecified(nameof(CommentsEnabled)))
            {
                if (CommentsEnabled)
                {
                    clientSidePage.EnableComments();
                }
                else
                {
                    clientSidePage.DisableComments();
                }
            }

            if (ContentType != null)
            {
                string ctId = ContentType.GetIdOrWarn(this, CurrentWeb);
                if (ctId != null)
                {
                    var pageFile = clientSidePage.GetPageFile(p => p.UniqueId, p => p.ListId, p => p.ListItemAllFields);
                    pageFile.ListItemAllFields["ContentTypeId"] = ctId;
                    pageFile.ListItemAllFields.SystemUpdate();
                }
            }

            if (Publish)
            {
                clientSidePage.Publish();
            }

            WriteObject(clientSidePage);
        }