/
NavigationImplementation.cs
159 lines (136 loc) · 7.92 KB
/
NavigationImplementation.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
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing.Navigation;
using Microsoft.SharePoint.Client.Taxonomy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PnP.Framework.Provisioning.Model;
using PnP.Framework.Provisioning.ObjectHandlers;
using PnP.Framework.Provisioning.Providers.Xml;
using PnP.Framework.Test.Framework.Functional.Validators;
using System;
namespace PnP.Framework.Test.Framework.Functional.Implementation
{
internal class NavigationImplementation : ImplementationBase
{
internal void SiteCollectionNavigation(string url)
{
using (var cc = TestCommon.CreateClientContext(url))
{
// Publishing needs to be activated for this test and we need a termset to be present
ImplementPrerequisites(cc);
#region Managed_Structural
// Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data.
ChangeNavigationSettings(cc, StandardNavigationSource.TaxonomyProvider, StandardNavigationSource.PortalProvider);
// Explicitely clear out the base template for this test as otherwise we're not getting any results back
ProvisioningTemplateCreationInformation ptci = new ProvisioningTemplateCreationInformation(cc.Web)
{
BaseTemplate = null,
HandlersToProcess = Handlers.Navigation,
};
var result = TestProvisioningTemplate(cc, "navigation_add_1605.xml", Handlers.Navigation, null, ptci);
NavigationValidator nv = new NavigationValidator
{
SchemaVersion = XMLConstants.PROVISIONING_SCHEMA_NAMESPACE_2021_03
};
Assert.IsTrue(nv.Validate(result.SourceTemplate.Navigation, result.TargetTemplate.Navigation, result.SourceTokenParser));
#endregion
#region Structural_Managed
ChangeNavigationSettings(cc, StandardNavigationSource.PortalProvider, StandardNavigationSource.TaxonomyProvider);
var result2 = TestProvisioningTemplate(cc, "navigation_add2_1605.xml", Handlers.Navigation, null, ptci);
NavigationValidator nv2 = new NavigationValidator
{
SchemaVersion = XMLConstants.PROVISIONING_SCHEMA_NAMESPACE_2021_03
};
Assert.IsTrue(nv2.Validate(result2.SourceTemplate.Navigation, result2.TargetTemplate.Navigation, result2.SourceTokenParser));
#endregion
}
}
internal void WebNavigation(string url)
{
using (var cc = TestCommon.CreateClientContext(url))
{
// Publishing needs to be activated for this test and we need a termset to be present
ImplementPrerequisites(cc);
#region Managed_Structural
// Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data.
ChangeNavigationSettings(cc, StandardNavigationSource.TaxonomyProvider, StandardNavigationSource.PortalProvider);
// Explicitely clear out the base template for this test as otherwise we're not getting any results back
ProvisioningTemplateCreationInformation ptci = new ProvisioningTemplateCreationInformation(cc.Web)
{
BaseTemplate = null,
HandlersToProcess = Handlers.Navigation,
};
var result = TestProvisioningTemplate(cc, "navigation_add_1605.xml", Handlers.Navigation, null, ptci);
NavigationValidator nv = new NavigationValidator
{
SchemaVersion = XMLConstants.PROVISIONING_SCHEMA_NAMESPACE_2021_03
};
Assert.IsTrue(nv.Validate(result.SourceTemplate.Navigation, result.TargetTemplate.Navigation, result.SourceTokenParser));
#endregion
#region Structural_Managed
ChangeNavigationSettings(cc, StandardNavigationSource.PortalProvider, StandardNavigationSource.TaxonomyProvider);
var result2 = TestProvisioningTemplate(cc, "navigation_add2_1605.xml", Handlers.Navigation, null, ptci);
NavigationValidator nv2 = new NavigationValidator
{
SchemaVersion = XMLConstants.PROVISIONING_SCHEMA_NAMESPACE_2021_03
};
Assert.IsTrue(nv2.Validate(result2.SourceTemplate.Navigation, result2.TargetTemplate.Navigation, result2.SourceTokenParser));
#endregion
#region Inherit_Structural
ChangeNavigationSettings(cc, StandardNavigationSource.InheritFromParentWeb, StandardNavigationSource.PortalProvider);
var result3 = TestProvisioningTemplate(cc, "navigation_add3_1605.xml", Handlers.Navigation, null, ptci);
NavigationValidator nv3 = new NavigationValidator
{
SchemaVersion = XMLConstants.PROVISIONING_SCHEMA_NAMESPACE_2021_03
};
Assert.IsTrue(nv3.Validate(result3.SourceTemplate.Navigation, result3.TargetTemplate.Navigation, result3.SourceTokenParser));
#endregion
}
}
#region Helper methods
// Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data.
public void ChangeNavigationSettings(ClientContext cc, StandardNavigationSource gSource, StandardNavigationSource cSource)
{
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc);
taxonomySession.UpdateCache();
cc.Load(taxonomySession, ts => ts.TermStores);
cc.ExecuteQueryRetry();
var navigationSettings = new WebNavigationSettings(cc, cc.Web);
navigationSettings.GlobalNavigation.Source = gSource;
navigationSettings.CurrentNavigation.Source = cSource;
navigationSettings.Update(taxonomySession);
try
{
cc.ExecuteQueryRetry();
}
catch (Exception) // if termset not found then set newly created termset to managed navigation
{
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
Microsoft.SharePoint.Client.Taxonomy.TermGroup group = termStore.GetTermGroupByName("TG_1"); // TG_1 is a term group mentioned in navigation_add_1605.xml
Microsoft.SharePoint.Client.Taxonomy.TermSet termset = group.TermSets.GetByName("TS_1_1"); // TS_1_1 is a term set mentioned in navigation_add_1605.xml
cc.Load(termStore);
cc.Load(group, g => g.TermSets);
cc.Load(termset);
cc.ExecuteQuery();
if (StandardNavigationSource.TaxonomyProvider == gSource)
{
navigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
navigationSettings.GlobalNavigation.TermSetId = termset.Id;
}
if (StandardNavigationSource.TaxonomyProvider == cSource)
{
navigationSettings.CurrentNavigation.TermStoreId = termStore.Id;
navigationSettings.CurrentNavigation.TermSetId = termset.Id;
}
navigationSettings.GlobalNavigation.Source = gSource;
navigationSettings.CurrentNavigation.Source = cSource;
navigationSettings.Update(taxonomySession);
cc.ExecuteQueryRetry();
}
}
private void ImplementPrerequisites(ClientContext cc)
{
TestProvisioningTemplate(cc, "navigation_add_prereq.xml", Handlers.TermGroups | Handlers.Features);
}
#endregion
}
}