-
Notifications
You must be signed in to change notification settings - Fork 352
/
AddPageImageWebPart.cs
109 lines (89 loc) · 4.25 KB
/
AddPageImageWebPart.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
100
101
102
103
104
105
106
107
108
109
using PnP.Core.Model.SharePoint;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Management.Automation;
namespace PnP.PowerShell.Commands.Pages
{
[Cmdlet(VerbsCommon.Add, "PnPPageImageWebPart")]
public class AddPageImageWebPart : PnPWebCmdlet
{
private const string ParameterSet_DEFAULT = "Default";
private const string ParameterSet_POSITIONED = "Positioned";
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, ParameterSetName = ParameterSet_POSITIONED)]
public PagePipeBind Page;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_POSITIONED)]
public string ImageUrl;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public int Order = 1;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_POSITIONED)]
public int Section;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_POSITIONED)]
public int Column;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public PageImageAlignment PageImageAlignment = PageImageAlignment.Center;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public int ImageWidth = 150;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public int ImageHeight = 150;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public string Caption = string.Empty;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public string AlternativeText = string.Empty;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEFAULT)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_POSITIONED)]
public string Link = string.Empty;
protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(Section)) && Section == 0)
{
throw new Exception("Section value should be at least 1 or higher");
}
if (ParameterSpecified(nameof(Column)) && Column == 0)
{
throw new Exception("Column value should be at least 1 or higher");
}
var clientSidePage = Page.GetPage(Connection);
if (clientSidePage == null)
{
// If the client side page object cannot be found
throw new Exception($"Page {Page} cannot be found.");
}
var imageControl = clientSidePage.GetImageWebPart(ImageUrl, new PageImageOptions()
{
Alignment = PageImageAlignment,
Height = ImageHeight,
Width = ImageWidth,
Caption = Caption,
AlternativeText = AlternativeText,
Link = Link
});
if (ParameterSpecified(nameof(Section)))
{
if (ParameterSpecified(nameof(Section)))
{
clientSidePage.AddControl(imageControl,
clientSidePage.Sections[Section - 1].Columns[Column - 1], Order);
}
else
{
clientSidePage.AddControl(imageControl, clientSidePage.Sections[Section - 1], Order);
}
}
else
{
clientSidePage.AddControl(imageControl, Order);
}
// Save the page
clientSidePage.Save();
WriteObject(imageControl);
}
}
}