/
PublishingAnalyzer.cs
289 lines (255 loc) · 14.1 KB
/
PublishingAnalyzer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using Microsoft.SharePoint.Client;
using SharePoint.Modernization.Scanner.Results;
using SharePoint.Scanning.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;
namespace SharePoint.Modernization.Scanner.Analyzers
{
public class PublishingAnalyzer : BaseAnalyzer
{
const string AvailablePageLayouts = "__PageLayouts";
const string DefaultPageLayout = "__DefaultPageLayout";
const string AvailableWebTemplates = "__WebTemplates";
const string InheritWebTemplates = "__InheritWebTemplates";
const string WebNavigationSettings = "_webnavigationsettings";
// Queries
const string CAMLQueryByExtension = @"
<View Scope='Recursive'>
<Query>
<Where>
<Contains>
<FieldRef Name='File_x0020_Type'/>
<Value Type='text'>aspx</Value>
</Contains>
</Where>
</Query>
</View>";
WebScanResult webScanResult;
SiteScanResult siteScanResult;
#region Construction
/// <summary>
/// Publishing analyzer construction
/// </summary>
/// <param name="url">Url of the web to be analyzed</param>
/// <param name="siteColUrl">Url of the site collection hosting this web</param>
public PublishingAnalyzer(string url, string siteColUrl, ModernizationScanJob scanJob, WebScanResult webScanResult, SiteScanResult siteScanResult) : base(url, siteColUrl, scanJob)
{
this.webScanResult = webScanResult;
this.siteScanResult = siteScanResult;
}
#endregion
public override TimeSpan Analyze(ClientContext cc)
{
try
{
base.Analyze(cc);
// Only scan when it's a valid publishing portal
var pageCount = ContinueScanning(cc);
if (pageCount > 0 || pageCount == -1)
{
PublishingScanResult scanResult = new PublishingScanResult()
{
SiteColUrl = this.SiteCollectionUrl,
SiteURL = this.SiteUrl,
WebRelativeUrl = this.SiteUrl.Replace(this.SiteCollectionUrl, ""),
WebTemplate = this.webScanResult.WebTemplate,
BrokenPermissionInheritance = this.webScanResult.BrokenPermissionInheritance,
PageCount = pageCount == -1 ? 0 : pageCount,
SiteMasterPage = this.webScanResult.CustomMasterPage,
SystemMasterPage = this.webScanResult.MasterPage,
AlternateCSS = this.webScanResult.AlternateCSS,
Admins = this.siteScanResult.Admins,
Owners = this.webScanResult.Owners,
};
Web web = cc.Web;
// Load additional web properties
web.EnsureProperties(p => p.Language);
scanResult.Language = web.Language;
// PageLayouts handling
var availablePageLayouts = web.GetPropertyBagValueString(AvailablePageLayouts, "");
var defaultPageLayout = web.GetPropertyBagValueString(DefaultPageLayout, "");
if (string.IsNullOrEmpty(availablePageLayouts))
{
scanResult.PageLayoutsConfiguration = "Any";
}
else if (availablePageLayouts.Equals("__inherit", StringComparison.InvariantCultureIgnoreCase))
{
scanResult.PageLayoutsConfiguration = "Inherit from parent";
}
else
{
scanResult.PageLayoutsConfiguration = "Defined list";
// Fill the defined list
var element = XElement.Parse(availablePageLayouts);
var nodes = element.Descendants("layout");
if (nodes != null && nodes.Count() > 0)
{
string allowedPageLayouts = "";
foreach (var node in nodes)
{
allowedPageLayouts = allowedPageLayouts + node.Attribute("url").Value.Replace("_catalogs/masterpage/", "") + ",";
}
allowedPageLayouts = allowedPageLayouts.TrimEnd(new char[] { ',' });
scanResult.AllowedPageLayouts = allowedPageLayouts;
}
}
if (!string.IsNullOrEmpty(defaultPageLayout))
{
var element = XElement.Parse(defaultPageLayout);
scanResult.DefaultPageLayout = element.Attribute("url").Value.Replace("_catalogs/masterpage/", "");
}
// Navigation
var navigationSettings = web.GetNavigationSettings();
if (navigationSettings != null)
{
if (navigationSettings.GlobalNavigation.ManagedNavigation)
{
scanResult.GlobalNavigationType = "Managed";
}
else
{
scanResult.GlobalNavigationType = "Structural";
scanResult.GlobalStruturalNavigationMaxCount = navigationSettings.GlobalNavigation.MaxDynamicItems;
scanResult.GlobalStruturalNavigationShowPages = navigationSettings.GlobalNavigation.ShowPages;
scanResult.GlobalStruturalNavigationShowSiblings = navigationSettings.GlobalNavigation.ShowSiblings;
scanResult.GlobalStruturalNavigationShowSubSites = navigationSettings.GlobalNavigation.ShowSubsites;
}
if (navigationSettings.CurrentNavigation.ManagedNavigation)
{
scanResult.CurrentNavigationType = "Managed";
}
else
{
scanResult.CurrentNavigationType = "Structural";
scanResult.CurrentStruturalNavigationMaxCount = navigationSettings.CurrentNavigation.MaxDynamicItems;
scanResult.CurrentStruturalNavigationShowPages = navigationSettings.CurrentNavigation.ShowPages;
scanResult.CurrentStruturalNavigationShowSiblings = navigationSettings.CurrentNavigation.ShowSiblings;
scanResult.CurrentStruturalNavigationShowSubSites = navigationSettings.CurrentNavigation.ShowSubsites;
}
if (navigationSettings.GlobalNavigation.ManagedNavigation || navigationSettings.CurrentNavigation.ManagedNavigation)
{
scanResult.ManagedNavigationAddNewPages = navigationSettings.AddNewPagesToNavigation;
scanResult.ManagedNavigationCreateFriendlyUrls = navigationSettings.CreateFriendlyUrlsForNewPages;
// get information about the managed nav term set configuration
var managedNavXml = web.GetPropertyBagValueString(WebNavigationSettings, "");
if (!string.IsNullOrEmpty(managedNavXml))
{
var managedNavSettings = XElement.Parse(managedNavXml);
IEnumerable<XElement> navNodes = managedNavSettings.XPathSelectElements("./SiteMapProviderSettings/TaxonomySiteMapProviderSettings");
foreach (var node in navNodes)
{
if (node.Attribute("Name").Value.Equals("CurrentNavigationTaxonomyProvider", StringComparison.InvariantCulture))
{
if (node.Attribute("TermSetId") != null)
{
scanResult.CurrentManagedNavigationTermSetId = node.Attribute("TermSetId").Value;
}
else if (node.Attribute("UseParentSiteMap") != null)
{
scanResult.CurrentManagedNavigationTermSetId = "Inherit from parent";
}
}
else if (node.Attribute("Name").Value.Equals("GlobalNavigationTaxonomyProvider", StringComparison.InvariantCulture))
{
if (node.Attribute("TermSetId") != null)
{
scanResult.GlobalManagedNavigationTermSetId = node.Attribute("TermSetId").Value;
}
else if (node.Attribute("UseParentSiteMap") != null)
{
scanResult.GlobalManagedNavigationTermSetId = "Inherit from parent";
}
}
}
}
}
}
// Pages library
var pagesLibrary = web.GetListsToScan().Where(p => p.BaseTemplate == 850).FirstOrDefault();
if (pagesLibrary != null)
{
pagesLibrary.EnsureProperties(p => p.EnableModeration, p => p.EnableVersioning, p => p.EnableMinorVersions, p => p.EventReceivers, p => p.Fields, p => p.DefaultContentApprovalWorkflowId);
scanResult.LibraryEnableModeration = pagesLibrary.EnableModeration;
scanResult.LibraryEnableVersioning = pagesLibrary.EnableVersioning;
scanResult.LibraryEnableMinorVersions = pagesLibrary.EnableMinorVersions;
scanResult.LibraryItemScheduling = pagesLibrary.ItemSchedulingEnabled();
scanResult.LibraryApprovalWorkflowDefined = pagesLibrary.DefaultContentApprovalWorkflowId != Guid.Empty;
}
// Variations
if (scanResult.Level == 0)
{
var variationLabels = cc.GetVariationLabels();
string labels = "";
string sourceLabel = "";
foreach(var label in variationLabels)
{
labels = labels + $"{label.Title} ({label.Language}),";
if (label.IsSource)
{
sourceLabel = label.Title;
}
}
scanResult.VariationLabels = labels.TrimEnd(new char[] { ',' }); ;
scanResult.VariationSourceLabel = sourceLabel;
}
// Persist publishing scan results
if (!this.ScanJob.PublishingScanResults.TryAdd(this.SiteUrl, scanResult))
{
ScanError error = new ScanError()
{
Error = $"Could not add publishing scan result for {this.SiteUrl}",
SiteColUrl = this.SiteCollectionUrl,
SiteURL = this.SiteUrl,
Field1 = "PublishingAnalyzer",
};
this.ScanJob.ScanErrors.Push(error);
}
}
}
finally
{
this.StopTime = DateTime.Now;
}
// return the duration of this scan
return new TimeSpan((this.StopTime.Subtract(this.StartTime).Ticks));
}
private int ContinueScanning(ClientContext cc)
{
// Check site collection
if (this.siteScanResult != null)
{
Web web = cc.Web;
// "Classic" publishing portal found
if ((this.siteScanResult.WebTemplate == "BLANKINTERNET#0" || this.siteScanResult.WebTemplate == "ENTERWIKI#0" || this.siteScanResult.WebTemplate == "SRCHCEN#0") &&
(this.siteScanResult.SitePublishingFeatureEnabled && this.siteScanResult.WebPublishingFeatureEnabled))
{
var pagesLibrary = web.GetListsToScan().Where(p => p.BaseTemplate == 850).FirstOrDefault();
if (pagesLibrary != null)
{
if (pagesLibrary.ItemCount > 0)
{
return pagesLibrary.ItemCount;
}
}
// always return a value in this case, if no pages found as this is a "classic" portal
return -1;
}
// Publishing enabled on non typical publishing portal site...check if there are pages in the Pages library
if (this.siteScanResult.SitePublishingFeatureEnabled && this.siteScanResult.WebPublishingFeatureEnabled)
{
var pagesLibrary = web.GetListsToScan().Where(p => p.BaseTemplate == 850).FirstOrDefault();
if (pagesLibrary != null)
{
return pagesLibrary.ItemCount;
}
}
}
return 0;
}
}
}