diff --git a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectHierarchySequenceTermGroups.cs b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectHierarchySequenceTermGroups.cs index 3061ca57f..7618a746d 100644 --- a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectHierarchySequenceTermGroups.cs +++ b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectHierarchySequenceTermGroups.cs @@ -1,14 +1,15 @@ -using Microsoft.Online.SharePoint.TenantAdministration; +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Online.SharePoint.TenantAdministration; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.Taxonomy; using PnP.Framework.Diagnostics; using PnP.Framework.Provisioning.Model; using PnP.Framework.Provisioning.Model.Configuration; using PnP.Framework.Provisioning.ObjectHandlers.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; using Term = Microsoft.SharePoint.Client.Taxonomy.Term; +using TermGroup = Microsoft.SharePoint.Client.Taxonomy.TermGroup; namespace PnP.Framework.Provisioning.ObjectHandlers { @@ -25,26 +26,46 @@ public override TokenParser ProvisionObjects(Tenant tenant, Model.ProvisioningHi { foreach (var sequence in hierarchy.Sequences) { - this.reusedTerms = new List(); var context = tenant.Context as ClientContext; TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(context); - TermStore termStore = null; + TermStore termStore; + IEnumerable termGroups; try { termStore = taxSession.GetDefaultKeywordsTermStore(); - context.Load(termStore, - ts => ts.Languages, - ts => ts.DefaultLanguage, - ts => ts.Groups.Include( - tg => tg.Name, - tg => tg.Id, - tg => tg.TermSets.Include( - tset => tset.Name, - tset => tset.Id))); + context.Load(termStore, ts => ts.Languages, ts => ts.DefaultLanguage); + + if (configuration.LoadSiteCollectionTermGroups) + { + termGroups = termStore.Groups; + + context.Load( + termStore.Groups, + groups => groups.Include( + group => group.Name, + group => group.Id, + group => group.TermSets.Include( + termSet => termSet.Name, + termSet => termSet.Id) + )); + } + else + { + termGroups = context.LoadQuery(termStore + .Groups + .Where(group => !group.IsSiteCollectionGroup) + .Include( + group => group.Name, + group => group.Id, + group => group.TermSets.Include( + termSet => termSet.Name, + termSet => termSet.Id))); + } + context.ExecuteQueryRetry(); } catch (ServerException) @@ -56,7 +77,7 @@ public override TokenParser ProvisionObjects(Tenant tenant, Model.ProvisioningHi foreach (var modelTermGroup in sequence.TermStore.TermGroups) { - this.reusedTerms.AddRange(TermGroupHelper.ProcessGroup(context, taxSession, termStore, modelTermGroup, null, parser, scope)); + this.reusedTerms.AddRange(TermGroupHelper.ProcessGroup(context, taxSession, termStore, termGroups, modelTermGroup, null, parser, scope)); } foreach (var reusedTerm in this.reusedTerms) diff --git a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectTermGroups.cs b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectTermGroups.cs index 07e4e23d5..8ec1df815 100644 --- a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectTermGroups.cs +++ b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectTermGroups.cs @@ -1,11 +1,10 @@ -using Microsoft.SharePoint.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.Taxonomy; using PnP.Framework.Diagnostics; -using PnP.Framework.Provisioning.ObjectHandlers.TokenDefinitions; using PnP.Framework.Provisioning.ObjectHandlers.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; namespace PnP.Framework.Provisioning.ObjectHandlers { @@ -24,22 +23,44 @@ public override TokenParser ProvisionObjects(Web web, Model.ProvisioningTemplate this.reusedTerms = new List(); TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(web.Context); - TermStore termStore = null; - TermGroup siteCollectionTermGroup = null; + TermStore termStore; + TermGroup siteCollectionTermGroup; + IEnumerable termGroups; try { termStore = taxSession.GetDefaultKeywordsTermStore(); - web.Context.Load(termStore, - ts => ts.Languages, - ts => ts.DefaultLanguage, - ts => ts.Groups.Include( - tg => tg.Name, - tg => tg.Id, - tg => tg.TermSets.Include( - tset => tset.Name, - tset => tset.Id))); + + web.Context.Load(termStore, ts => ts.Languages, ts => ts.DefaultLanguage); siteCollectionTermGroup = termStore.GetSiteCollectionGroup((web.Context as ClientContext).Site, false); + + if (applyingInformation.LoadSiteCollectionTermGroups) + { + termGroups = termStore.Groups; + + web.Context.Load( + termStore.Groups, + groups => groups.Include( + group => group.Name, + group => group.Id, + group => group.TermSets.Include( + termSet => termSet.Name, + termSet => termSet.Id) + )); + } + else + { + termGroups = web.Context.LoadQuery(termStore + .Groups + .Where(group => !group.IsSiteCollectionGroup) + .Include( + group => group.Name, + group => group.Id, + group => group.TermSets.Include( + termSet => termSet.Name, + termSet => termSet.Id))); + } + web.Context.Load(siteCollectionTermGroup); web.Context.ExecuteQueryRetry(); } @@ -52,12 +73,9 @@ public override TokenParser ProvisionObjects(Web web, Model.ProvisioningTemplate return parser; } - SiteCollectionTermGroupNameToken siteCollectionTermGroupNameToken = - new SiteCollectionTermGroupNameToken(web); - foreach (var modelTermGroup in template.TermGroups) { - this.reusedTerms.AddRange(TermGroupHelper.ProcessGroup(web.Context as ClientContext, taxSession, termStore, modelTermGroup, siteCollectionTermGroup, parser, scope)); + this.reusedTerms.AddRange(TermGroupHelper.ProcessGroup(web.Context as ClientContext, taxSession, termStore, termGroups, modelTermGroup, siteCollectionTermGroup, parser, scope)); } foreach (var reusedTerm in this.reusedTerms) diff --git a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/Utilities/TermGroupHelper.cs b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/Utilities/TermGroupHelper.cs index b10b32fc2..869142b84 100644 --- a/src/lib/PnP.Framework/Provisioning/ObjectHandlers/Utilities/TermGroupHelper.cs +++ b/src/lib/PnP.Framework/Provisioning/ObjectHandlers/Utilities/TermGroupHelper.cs @@ -10,7 +10,7 @@ namespace PnP.Framework.Provisioning.ObjectHandlers.Utilities { internal static class TermGroupHelper { - internal static List ProcessGroup(ClientContext context, TaxonomySession session, TermStore termStore, Model.TermGroup modelTermGroup, TermGroup siteCollectionTermGroup, TokenParser parser, PnPMonitoredScope scope) + internal static List ProcessGroup(ClientContext context, TaxonomySession session, TermStore termStore, IEnumerable termGroups, Model.TermGroup modelTermGroup, TermGroup siteCollectionTermGroup, TokenParser parser, PnPMonitoredScope scope) { List reusedTerms = new List(); @@ -26,7 +26,7 @@ internal static List ProcessGroup(ClientContext context, TaxonomySes var normalizedGroupName = TaxonomyItem.NormalizeName(context, modelGroupName); context.ExecuteQueryRetry(); - TermGroup group = termStore.Groups.FirstOrDefault( + TermGroup group = termGroups.FirstOrDefault( g => g.Id == modelTermGroup.Id || g.Name == normalizedGroupName.Value); if (group == null) { @@ -45,7 +45,7 @@ internal static List ProcessGroup(ClientContext context, TaxonomySes } else { - group = termStore.Groups.FirstOrDefault(g => g.Name == normalizedGroupName.Value); + group = termGroups.FirstOrDefault(g => g.Name == normalizedGroupName.Value); if (group == null) {