-
Notifications
You must be signed in to change notification settings - Fork 3
/
SetIaCCustomAction.cs
121 lines (109 loc) · 4.66 KB
/
SetIaCCustomAction.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
110
111
112
113
114
115
116
117
118
119
120
121
using InfrastructureAsCode.Core.Extensions;
using InfrastructureAsCode.Core.Models;
using InfrastructureAsCode.Core.Utilities;
using InfrastructureAsCode.Powershell.Commands.Base;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace InfrastructureAsCode.Powershell.Commands.Sites
{
/// <summary>
/// The function cmdlet will allow you to specify a JSON file and update the Site or Web with the appropriate User Custom Actions
/// </summary>
/// <remarks>
/// Set-IaCCustomAction -FilePath c:\filedir\customaction.json -Verbose
/// </remarks>
[Cmdlet(VerbsCommon.Set, "IaCCustomAction", SupportsShouldProcess = true)]
public class SetIaCCustomAction : IaCCmdlet
{
/// <summary>
/// Full path to the JSON file
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = "Default")]
public string FilePath { get; set; }
/// <summary>
/// Evaluate the full path
/// </summary>
protected override void OnBeginInitialize()
{
if (this.ParameterSetName == "Default")
{
var fileInfo = new System.IO.FileInfo(FilePath);
if (!fileInfo.Exists)
{
throw new System.IO.FileNotFoundException("File not found", fileInfo.Name);
}
}
}
public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
var site = this.ClientContext.Site;
var web = this.ClientContext.Web;
this.ClientContext.Load(site, ccsu => ccsu.ServerRelativeUrl, cssu => cssu.UserCustomActions);
this.ClientContext.Load(web, ccwu => ccwu.ServerRelativeUrl, ccwu => ccwu.UserCustomActions);
this.ClientContext.ExecuteQueryRetry();
var siteurl = TokenHelper.EnsureTrailingSlash(site.ServerRelativeUrl);
var weburl = TokenHelper.EnsureTrailingSlash(web.ServerRelativeUrl);
var fileInfo = new System.IO.FileInfo(FilePath);
var actions = JsonConvert.DeserializeObject<SPCustomAction>(System.IO.File.ReadAllText(fileInfo.FullName));
if (actions.Site != null)
{
if (actions.Site.scriptblocks != null && actions.Site.scriptblocks.Any())
{
actions.Site.scriptblocks.ForEach(cab =>
{
var htmlblock = cab.htmlblock.Replace("~SiteCollection/", siteurl);
htmlblock = htmlblock.Replace("~Site/", weburl);
site.AddOrUpdateCustomActionLinkBlock(cab.name, htmlblock, cab.sequence);
});
}
if (actions.Site.scriptlinks != null && actions.Site.scriptlinks.Any())
{
actions.Site.scriptlinks.ForEach(cab =>
{
site.AddOrUpdateCustomActionLink(cab.name, cab.linkurl, cab.sequence);
});
}
}
if (actions.Web != null)
{
if (actions.Web.scriptblocks != null && actions.Web.scriptblocks.Any())
{
actions.Web.scriptblocks.ForEach(cab =>
{
var htmlblock = cab.htmlblock.Replace("~SiteCollection/", siteurl);
htmlblock = htmlblock.Replace("~Site/", weburl);
web.AddOrUpdateCustomActionLinkBlock(cab.name, htmlblock, cab.sequence);
});
}
if (actions.Web.scriptlinks != null && actions.Web.scriptlinks.Any())
{
actions.Web.scriptlinks.ForEach(cab =>
{
web.AddOrUpdateCustomActionLink(cab.name, cab.linkurl, cab.sequence);
});
}
}
if (actions.List != null && actions.List.Any())
{
foreach (var list in actions.List)
{
var weblist = web.GetListByTitle(list.Title);
foreach (var listaction in list.scriptcommands)
{
var htmllink = listaction.ImageUrl.Replace("~SiteCollection/", siteurl);
htmllink = htmllink.Replace("~Site/", weburl);
listaction.ImageUrl = htmllink;
weblist.AddOrUpdateCustomActionLink(listaction);
}
}
}
}
}
}