This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/
AddClientSidePage.cs
99 lines (82 loc) · 3.43 KB
/
AddClientSidePage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#if !ONPREMISES
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using System;
using System.Management.Automation;
namespace SharePointPnP.PowerShell.Commands.ClientSidePages
{
[Cmdlet(VerbsCommon.Add, "PnPClientSidePage")]
[CmdletHelp("Adds a Client-Side Page",
Category = CmdletHelpCategory.ClientSidePages, SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Add-PnPClientSidePage -PageName ""OurNewPage""",
Remarks = "Creates a new Client-Side page called 'OurNewPage'",
SortOrder = 1)]
public class AddClientSidePage : PnPWebCmdlet
{
[Parameter(Mandatory = true, HelpMessage = "Specifies the name of the page.")]
public string Name = null;
[Parameter(Mandatory = false, HelpMessage = "Specifies the layout type of the page.")]
public ClientSidePageLayoutType LayoutType = ClientSidePageLayoutType.Article;
[Parameter(Mandatory = false, HelpMessage = "Allows to promote the page for a specific purpose (HomePage | NewsPage)")]
public ClientSidePagePromoteType PromoteAs = ClientSidePagePromoteType.None;
[Parameter(Mandatory = false, HelpMessage = "Enables or Disables the comments on the page")]
public bool? CommentsEnabled = null;
[Parameter(Mandatory = false, HelpMessage = "Publishes the page once it is saved. Applicable to libraries set to create major and minor versions.")]
public SwitchParameter Publish;
[Parameter(Mandatory = false, HelpMessage = "Sets the message for publishing the page.")]
public string PublishMessage = string.Empty;
protected override void ExecuteCmdlet()
{
ClientSidePage clientSidePage = null;
// Check if the page exists
string name = ClientSidePageUtilities.EnsureCorrectPageName(Name);
bool pageExists = false;
try
{
ClientSidePage.Load(ClientContext, name);
pageExists = true;
}
catch { }
if(pageExists)
{
throw new Exception($"Page {name} already exists");
}
// Create a page that persists immediately
clientSidePage = SelectedWeb.AddClientSidePage(name);
clientSidePage.LayoutType = LayoutType;
clientSidePage.Save(name);
// If a specific promote type is specified, promote the page as Home or Article or ...
switch (PromoteAs)
{
case ClientSidePagePromoteType.HomePage:
clientSidePage.PromoteAsHomePage();
break;
case ClientSidePagePromoteType.NewsArticle:
clientSidePage.PromoteAsNewsArticle();
break;
case ClientSidePagePromoteType.None:
default:
break;
}
if (CommentsEnabled.HasValue)
{
if (CommentsEnabled.Value)
{
clientSidePage.EnableComments();
}
else
{
clientSidePage.DisableComments();
}
}
if (Publish)
{
clientSidePage.Publish(PublishMessage);
}
WriteObject(clientSidePage);
}
}
}
#endif