Skip to content

Commit

Permalink
Extended term group loading behavior
Browse files Browse the repository at this point in the history
"ObjectTermGroups" and "ObjectHierarchySequenceTermGroups" now also
respect "LoadSiteCollectionTermGroups" when loading term groups.
  • Loading branch information
fzbm committed Oct 15, 2024
1 parent 8b1655d commit 457755c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -25,26 +26,46 @@ public override TokenParser ProvisionObjects(Tenant tenant, Model.ProvisioningHi
{
foreach (var sequence in hierarchy.Sequences)
{

this.reusedTerms = new List<TermGroupHelper.ReusedTerm>();

var context = tenant.Context as ClientContext;

TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(context);
TermStore termStore = null;
TermStore termStore;
IEnumerable<TermGroup> 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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -24,22 +23,44 @@ public override TokenParser ProvisionObjects(Web web, Model.ProvisioningTemplate
this.reusedTerms = new List<TermGroupHelper.ReusedTerm>();

TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(web.Context);
TermStore termStore = null;
TermGroup siteCollectionTermGroup = null;
TermStore termStore;
TermGroup siteCollectionTermGroup;
IEnumerable<TermGroup> 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();
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PnP.Framework.Provisioning.ObjectHandlers.Utilities
{
internal static class TermGroupHelper
{
internal static List<ReusedTerm> ProcessGroup(ClientContext context, TaxonomySession session, TermStore termStore, Model.TermGroup modelTermGroup, TermGroup siteCollectionTermGroup, TokenParser parser, PnPMonitoredScope scope)
internal static List<ReusedTerm> ProcessGroup(ClientContext context, TaxonomySession session, TermStore termStore, IEnumerable<TermGroup> termGroups, Model.TermGroup modelTermGroup, TermGroup siteCollectionTermGroup, TokenParser parser, PnPMonitoredScope scope)
{
List<ReusedTerm> reusedTerms = new List<ReusedTerm>();

Expand All @@ -26,7 +26,7 @@ internal static List<ReusedTerm> 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)
{
Expand All @@ -45,7 +45,7 @@ internal static List<ReusedTerm> 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)
{
Expand Down

0 comments on commit 457755c

Please sign in to comment.