예제 #1
0
        /// <summary>
        /// Writes a taxonomy field value to a SPListItem.
        /// </summary>
        /// <param name="item">The SharePoint List Item</param>
        /// <param name="fieldValueInfo">The field and value information</param>
        public override void WriteValueToListItem(SPListItem item, FieldValueInfo fieldValueInfo)
        {
            var termInfo = fieldValueInfo.Value as TaxonomyValue;
            TaxonomyFieldValue newTaxonomyFieldValue = null;

            TaxonomyField taxonomyField = (TaxonomyField)item.Fields.GetField(fieldValueInfo.FieldInfo.InternalName);

            newTaxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);

            var noteField = item.Fields[taxonomyField.TextField];

            if (termInfo != null && termInfo.Term != null)
            {
                string labelGuidPair = TaxonomyItem.NormalizeName(termInfo.Term.Label) + TaxonomyField.TaxonomyGuidLabelDelimiter + termInfo.Term.Id.ToString().ToUpperInvariant();

                // PopulateFromLabelGuidPair takes care of looking up the WssId value and creating a new item in the TaxonomyHiddenList if needed.
                // Main taxonomy field value format: WssID;#Label
                // TODO - Make sure we support sub-level terms with format: WssID;#Label|RootTermGuid|...|ParentTermGuid|TermGuid
                // Reference: http://msdn.microsoft.com/en-us/library/ee567833.aspx
                newTaxonomyFieldValue.PopulateFromLabelGuidPair(labelGuidPair);

                // Must write associated note field as well as the main taxonomy field.
                // Note field value format: Label|Guid
                // Reference: http://nickhobbs.wordpress.com/2012/02/21/sharepoint-2010-how-to-set-taxonomy-field-values-programmatically/
                item[noteField.InternalName] = labelGuidPair;
            }
            else
            {
                // No taxonomy value, make sure to empty the note field as well
                item[noteField.InternalName] = null;
            }

            item[fieldValueInfo.FieldInfo.InternalName] = newTaxonomyFieldValue;
        }
        private static TaxonomyFieldValueCollection CreateSharePointTaxonomyFieldValue(
            TaxonomyField sharepointTaxonomyField,
            TaxonomyValueCollection dynamiteCollection,
            List <string> labelGuidPairsListOutParam)
        {
            if (labelGuidPairsListOutParam == null)
            {
                labelGuidPairsListOutParam = new List <string>();
            }

            TaxonomyFieldValueCollection sharePointValueCollection = null;

            foreach (var TaxonomyValue in dynamiteCollection)
            {
                string labelGuidPair = TaxonomyItem.NormalizeName(TaxonomyValue.Term.Label) + TaxonomyField.TaxonomyGuidLabelDelimiter
                                       + TaxonomyValue.Term.Id.ToString().ToUpperInvariant();

                labelGuidPairsListOutParam.Add(labelGuidPair);
            }

            if (labelGuidPairsListOutParam.Count >= 1)
            {
                sharePointValueCollection = new TaxonomyFieldValueCollection(sharepointTaxonomyField);

                labelGuidPairsListOutParam.ForEach(labelGuidPair =>
                {
                    TaxonomyFieldValue taxoFieldValue = new TaxonomyFieldValue(sharepointTaxonomyField);
                    taxoFieldValue.PopulateFromLabelGuidPair(labelGuidPair);

                    sharePointValueCollection.Add(taxoFieldValue);
                });
            }

            return(sharePointValueCollection);
        }
예제 #3
0
        private static void UpdateTerm(TermSetItem termSet, ManagedMetadataEntity entity, IEnumerable <IEnumerable <ManagedMetadataEntity> > termLevels, int currLevel)
        {
            int    lcid     = entity.LCID.HasValue ? entity.LCID.Value : 1033;
            string termName = TaxonomyItem.NormalizeName(entity.GetTermLevel(currLevel));

            if (string.IsNullOrEmpty(termName))
            {
                return;
            }

            Term term = termSet is TermSet
                            ? (termSet as TermSet).GetTerms(termName, lcid, true, StringMatchOption.ExactMatch, 1, false).
                        FirstOrDefault()
                            : termSet.Terms.FirstOrDefault(t => t.Name.Equals(termName, StringComparison.InvariantCulture));

            if (term == null)
            {
                //try
                //{
                term = termSet.CreateTerm(termName, lcid, Guid.NewGuid());
                term.IsAvailableForTagging = entity.AvailableForTagging;
                //}
                //catch (Exception ex)
                //{
                //}
            }

            LabelCollection allLabels = term.GetAllLabels(lcid);

            if (allLabels.Count == 0 || !allLabels.Select(x => x.Value).Contains(termName))
            {
                //try
                //{
                term.CreateLabel(termName, lcid, true);
                term.SetDescription(entity.TermDescription, lcid);
                //}
                //catch (Exception ex)
                //{
                //}
            }

            if (termLevels == null)
            {
                return;
            }
            if (currLevel >= termLevels.Count())
            {
                return;
            }

            IEnumerable <ManagedMetadataEntity> childList =
                termLevels.ElementAt(currLevel).Where(
                    t => t.HasSameLevelTerm(currLevel, entity));

            foreach (ManagedMetadataEntity childEntity in childList)
            {
                UpdateTerm(term, childEntity, termLevels, currLevel + 1);
            }
        }
예제 #4
0
        private TokenParser CheckChildTerms(Web web, Model.Term modelTerm, Term parentTerm, TermStore termStore, TokenParser parser, PnPMonitoredScope scope)
        {
            if (modelTerm.Terms.Any())
            {
                parentTerm.Context.Load(parentTerm, s => s.Terms.Include(t => t.Id, t => t.Name));
                parentTerm.Context.ExecuteQueryRetry();

                var terms = parentTerm.Terms;

                foreach (var childTerm in modelTerm.Terms)
                {
                    if (terms.Any())
                    {
                        var term = terms.FirstOrDefault(t => t.Id == childTerm.Id);
                        if (term == null)
                        {
                            var normalizedTermName = TaxonomyItem.NormalizeName(web.Context, childTerm.Name);
                            web.Context.ExecuteQueryRetry();

                            term = terms.FirstOrDefault(t => t.Name == normalizedTermName.Value);
                            if (term == null)
                            {
                                var returnTuple = CreateTerm <TermSet>(web, childTerm, parentTerm, termStore, parser, scope);
                                if (returnTuple != null)
                                {
                                    childTerm.Id = returnTuple.Item1;
                                    parser       = returnTuple.Item2;
                                }
                            }
                            else
                            {
                                childTerm.Id = term.Id;
                            }
                        }
                        else
                        {
                            childTerm.Id = term.Id;
                        }

                        if (term != null)
                        {
                            parser = CheckChildTerms(web, childTerm, term, termStore, parser, scope);
                        }
                    }
                    else
                    {
                        var returnTuple = CreateTerm <TermSet>(web, childTerm, parentTerm, termStore, parser, scope);
                        if (returnTuple != null)
                        {
                            childTerm.Id = returnTuple.Item1;
                            parser       = returnTuple.Item2;
                        }
                    }
                }
            }

            return(parser);
        }
예제 #5
0
        private static string FormatTaxonomyString(TaxonomyField sharePointField, TaxonomyValue valueToApply)
        {
            var    sharePointTaxonomyFieldValue = new TaxonomyFieldValue(sharePointField);
            string path = TaxonomyItem.NormalizeName(valueToApply.Term.Label) + TaxonomyField.TaxonomyGuidLabelDelimiter
                          + valueToApply.Term.Id.ToString().ToUpperInvariant();

            sharePointTaxonomyFieldValue.PopulateFromLabelGuidPair(path);

            return(sharePointTaxonomyFieldValue.ValidatedString);
        }
예제 #6
0
        // STEP 3: Update the term properties, including correcting any
        // temporary names that were introduced in step 1.
        static void ProcessPropertyUpdates(TermSetItem parentItem, ItemGoal parentItemGoal)
        {
            foreach (Term term in parentItem.Terms)
            {
                TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == term.Id);
                if (termGoal == null)
                {
                    continue;  // This is a term that would have been deleted by the ProcessDeletes() method.
                }
                // -----------------
                string goalName = TaxonomyItem.NormalizeName(termGoal.Name);
                if (term.Name != goalName)
                {
                    Log("* Renaming term from \"" + term.Name + "\" to \"" + termGoal.Name + "\"");
                    term.Name = goalName;
                }

                HashSet <string> labels = new HashSet <string>(
                    term.Labels.Where(l => !l.IsDefaultForLanguage).Select(l => l.Value));

                HashSet <string> labelsGoal = new HashSet <string>(termGoal.OtherLabels);

                // Delete any extra labels.
                foreach (string label in labels.Except(labelsGoal))
                {
                    Log("* Term \"" + term.Name + "\": Deleting label \"" + label + "\"");
                    term.Labels.First(l => l.Value == label).Delete();
                }

                // Add any missing labels.
                foreach (string label in labelsGoal.Except(labels))
                {
                    Log("* Term \"" + term.Name + "\": Adding label \"" + label + "\"");
                    term.CreateLabel(label, lcid, isDefault: false);
                }

                if (term.GetDescription() != termGoal.Description)
                {
                    Log("* Term \"" + term.Name + "\": Updating description");
                    term.SetDescription(termGoal.Description, lcid);
                }

                if (term.IsDeprecated != termGoal.IsDeprecated)
                {
                    Log("* Term \"" + term.Name + "\": Marking as "
                        + (termGoal.IsDeprecated ? "Deprecated" : "Not deprecated"));
                    term.Deprecate(termGoal.IsDeprecated);
                }
                // -----------------

                ProcessPropertyUpdates(term, termGoal); // Recurse.
            }
        }
예제 #7
0
        public static TaxonomyFieldValue GetTaxonomyFieldValue(TermSet termSet, string value, bool addIfDoesNotExist, out bool newTermAdded)
        {
            string termVal = TaxonomyItem.NormalizeName((value ?? string.Empty).Trim()); //ReplaceIllegalCharacters((value ?? string.Empty).Trim());
            Term   term    = null;

            newTermAdded = false;

            if (termSet != null)
            {
                if (!string.IsNullOrEmpty(termVal))
                {
                    term = termSet.GetTerms(termVal, termSet.TermStore.DefaultLanguage, true,
                                            StringMatchOption.ExactMatch, 1, false).FirstOrDefault();
                }

                if (term == null && termSet.IsOpenForTermCreation && addIfDoesNotExist)
                {
                    if (!string.IsNullOrEmpty(termVal))
                    {
                        term         = termSet.CreateTerm(termVal, termSet.TermStore.DefaultLanguage);
                        newTermAdded = true;
                        //termSet.TermStore.CommitAll();
                    }
                }

                if (term != null)
                {
                    string termValue = string.Concat(term.GetDefaultLabel(termSet.TermStore.DefaultLanguage),
                                                     TaxonomyField.TaxonomyGuidLabelDelimiter,
                                                     term.Id.ToString());
                    return(TaxonomyFieldControl.GetTaxonomyValue(termValue));
                }
            }

            return(null);
        }
예제 #8
0
        public override TokenParser ProvisionObjects(Web web, Model.ProvisioningTemplate template, TokenParser parser,
                                                     ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                this.reusedTerms = new List <ReusedTerm>();

                TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(web.Context);
                TermStore       termStore  = null;
                TermGroup       siteCollectionTermGroup = null;

                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)));
                    siteCollectionTermGroup = termStore.GetSiteCollectionGroup((web.Context as ClientContext).Site, false);
                    web.Context.Load(siteCollectionTermGroup);
                    web.Context.ExecuteQueryRetry();
                }
                catch (ServerException)
                {
                    // If the GetDefaultSiteCollectionTermStore method call fails ... raise a specific Warning
                    WriteMessage(CoreResources.Provisioning_ObjectHandlers_TermGroups_Wrong_Configuration, ProvisioningMessageType.Warning);

                    // and exit skipping the current handler
                    return(parser);
                }

                SiteCollectionTermGroupNameToken siteCollectionTermGroupNameToken =
                    new SiteCollectionTermGroupNameToken(web);

                foreach (var modelTermGroup in template.TermGroups)
                {
                    #region Group

                    var newGroup            = false;
                    var normalizedGroupName = TaxonomyItem.NormalizeName(web.Context, modelTermGroup.Name);
                    web.Context.ExecuteQueryRetry();

                    TermGroup group = termStore.Groups.FirstOrDefault(
                        g => g.Id == modelTermGroup.Id || g.Name == normalizedGroupName.Value);
                    if (group == null)
                    {
                        var parsedGroupName   = parser.ParseString(modelTermGroup.Name);
                        var parsedDescription = parser.ParseString(modelTermGroup.Description);

                        if (modelTermGroup.Name == "Site Collection" ||
                            parsedGroupName == siteCollectionTermGroupNameToken.GetReplaceValue() ||
                            modelTermGroup.SiteCollectionTermGroup)
                        {
                            var site = (web.Context as ClientContext).Site;
                            group = termStore.GetSiteCollectionGroup(site, true);
                            web.Context.Load(group, g => g.Name, g => g.Id, g => g.TermSets.Include(
                                                 tset => tset.Name,
                                                 tset => tset.Id));
                            web.Context.ExecuteQueryRetry();
                        }
                        else
                        {
                            var parsedNormalizedGroupName = TaxonomyItem.NormalizeName(web.Context, parsedGroupName);
                            web.Context.ExecuteQueryRetry();

                            group = termStore.Groups.FirstOrDefault(g => g.Name == parsedNormalizedGroupName.Value);

                            if (group == null)
                            {
                                if (modelTermGroup.Id == Guid.Empty)
                                {
                                    modelTermGroup.Id = Guid.NewGuid();
                                }
                                group = termStore.CreateGroup(parsedGroupName, modelTermGroup.Id);

                                group.Description = parsedDescription;

#if !ONPREMISES
                                // Handle TermGroup Contributors, if any
                                if (modelTermGroup.Contributors != null && modelTermGroup.Contributors.Count > 0)
                                {
                                    foreach (var c in modelTermGroup.Contributors)
                                    {
                                        group.AddContributor(c.Name);
                                    }
                                }

                                // Handle TermGroup Managers, if any
                                if (modelTermGroup.Managers != null && modelTermGroup.Managers.Count > 0)
                                {
                                    foreach (var m in modelTermGroup.Managers)
                                    {
                                        group.AddGroupManager(m.Name);
                                    }
                                }
#endif

                                termStore.CommitAll();
                                web.Context.Load(group);
                                web.Context.ExecuteQueryRetry();

                                newGroup = true;
                            }
                        }
                    }

                    #endregion

                    #region TermSets

                    foreach (var modelTermSet in modelTermGroup.TermSets)
                    {
                        TermSet set        = null;
                        var     newTermSet = false;

                        var normalizedTermSetName = TaxonomyItem.NormalizeName(web.Context, modelTermSet.Name);
                        web.Context.ExecuteQueryRetry();

                        if (!newGroup)
                        {
                            set =
                                group.TermSets.FirstOrDefault(
                                    ts => ts.Id == modelTermSet.Id || ts.Name == normalizedTermSetName.Value);
                        }
                        if (set == null)
                        {
                            if (modelTermSet.Id == Guid.Empty)
                            {
                                modelTermSet.Id = Guid.NewGuid();
                            }
                            set = group.CreateTermSet(parser.ParseString(modelTermSet.Name), modelTermSet.Id,
                                                      modelTermSet.Language ?? termStore.DefaultLanguage);
                            parser.AddToken(new TermSetIdToken(web, group.Name, modelTermSet.Name, modelTermSet.Id));
                            if (!siteCollectionTermGroup.ServerObjectIsNull.Value)
                            {
                                if (group.Name == siteCollectionTermGroup.Name)
                                {
                                    parser.AddToken((new SiteCollectionTermSetIdToken(web, modelTermSet.Name, modelTermSet.Id)));
                                }
                            }
                            newTermSet                = true;
                            set.Description           = parser.ParseString(modelTermSet.Description);
                            set.IsOpenForTermCreation = modelTermSet.IsOpenForTermCreation;
                            set.IsAvailableForTagging = modelTermSet.IsAvailableForTagging;
                            foreach (var property in modelTermSet.Properties)
                            {
                                set.SetCustomProperty(property.Key, parser.ParseString(property.Value));
                            }
                            if (modelTermSet.Owner != null)
                            {
                                set.Owner = modelTermSet.Owner;
                            }
                            termStore.CommitAll();
                            web.Context.Load(set);
                            web.Context.ExecuteQueryRetry();
                        }

                        web.Context.Load(set, s => s.Terms.Include(t => t.Id, t => t.Name));
                        web.Context.ExecuteQueryRetry();
                        var terms = set.Terms;

                        foreach (var modelTerm in modelTermSet.Terms)
                        {
                            if (!newTermSet)
                            {
                                if (terms.Any())
                                {
                                    var term = terms.FirstOrDefault(t => t.Id == modelTerm.Id);
                                    if (term == null)
                                    {
                                        var normalizedTermName = TaxonomyItem.NormalizeName(web.Context, modelTerm.Name);
                                        web.Context.ExecuteQueryRetry();

                                        term = terms.FirstOrDefault(t => t.Name == normalizedTermName.Value);
                                        if (term == null)
                                        {
                                            var returnTuple = CreateTerm <TermSet>(web, modelTerm, set, termStore, parser, scope);
                                            if (returnTuple != null)
                                            {
                                                modelTerm.Id = returnTuple.Item1;
                                                parser       = returnTuple.Item2;
                                            }
                                        }
                                        else
                                        {
                                            modelTerm.Id = term.Id;
                                        }
                                    }
                                    else
                                    {
                                        modelTerm.Id = term.Id;
                                    }

                                    if (term != null)
                                    {
                                        CheckChildTerms(web, modelTerm, term, termStore, parser, scope);
                                    }
                                }
                                else
                                {
                                    var returnTuple = CreateTerm <TermSet>(web, modelTerm, set, termStore, parser, scope);
                                    if (returnTuple != null)
                                    {
                                        modelTerm.Id = returnTuple.Item1;
                                        parser       = returnTuple.Item2;
                                    }
                                }
                            }
                            else
                            {
                                var returnTuple = CreateTerm <TermSet>(web, modelTerm, set, termStore, parser, scope);
                                if (returnTuple != null)
                                {
                                    modelTerm.Id = returnTuple.Item1;
                                    parser       = returnTuple.Item2;
                                }
                            }
                        }

                        // do we need custom sorting?
                        if (modelTermSet.Terms.Any(t => t.CustomSortOrder > -1))
                        {
                            var sortedTerms = modelTermSet.Terms.OrderBy(t => t.CustomSortOrder);

                            var customSortString = sortedTerms.Aggregate(string.Empty,
                                                                         (a, i) => a + i.Id.ToString() + ":");
                            customSortString = customSortString.TrimEnd(new[] { ':' });

                            set.CustomSortOrder = customSortString;
                            termStore.CommitAll();
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    #endregion
                }

                foreach (var reusedTerm in this.reusedTerms)
                {
                    TryReuseTerm(web, reusedTerm.ModelTerm, reusedTerm.Parent, reusedTerm.TermStore, parser, scope);
                }
            }
            return(parser);
        }
예제 #9
0
        internal static List <ReusedTerm> ProcessGroup(ClientContext context, TaxonomySession session, TermStore termStore, Model.TermGroup modelTermGroup, TermGroup siteCollectionTermGroup, TokenParser parser, PnPMonitoredScope scope)
        {
            List <ReusedTerm> reusedTerms = new List <ReusedTerm>();

            SiteCollectionTermGroupNameToken siteCollectionTermGroupNameToken =
                new SiteCollectionTermGroupNameToken(context.Web);

            #region Group

            var newGroup = false;

            var modelGroupName = parser.ParseString(modelTermGroup.Name);

            var normalizedGroupName = TaxonomyItem.NormalizeName(context, modelGroupName);
            context.ExecuteQueryRetry();

            TermGroup group = termStore.Groups.FirstOrDefault(
                g => g.Id == modelTermGroup.Id || g.Name == normalizedGroupName.Value);
            if (group == null)
            {
                var parsedDescription = parser.ParseString(modelTermGroup.Description);

                if (modelTermGroup.Name == "Site Collection" ||
                    modelGroupName == siteCollectionTermGroupNameToken.GetReplaceValue() ||
                    modelTermGroup.SiteCollectionTermGroup)
                {
                    var site = (context as ClientContext).Site;
                    group = termStore.GetSiteCollectionGroup(site, true);
                    context.Load(group, g => g.Name, g => g.Id, g => g.TermSets.Include(
                                     tset => tset.Name,
                                     tset => tset.Id));
                    context.ExecuteQueryRetry();
                }
                else
                {
                    group = termStore.Groups.FirstOrDefault(g => g.Name == normalizedGroupName.Value);

                    if (group == null)
                    {
                        if (modelTermGroup.Id == Guid.Empty)
                        {
                            modelTermGroup.Id = Guid.NewGuid();
                        }
                        group = termStore.CreateGroup(modelGroupName, modelTermGroup.Id);

                        group.Description = parsedDescription;

                        // Handle TermGroup Contributors, if any
                        if (modelTermGroup.Contributors != null && modelTermGroup.Contributors.Count > 0)
                        {
                            foreach (var c in modelTermGroup.Contributors)
                            {
                                group.AddContributor(c.Name);
                            }
                        }

                        // Handle TermGroup Managers, if any
                        if (modelTermGroup.Managers != null && modelTermGroup.Managers.Count > 0)
                        {
                            foreach (var m in modelTermGroup.Managers)
                            {
                                group.AddGroupManager(m.Name);
                            }
                        }

                        termStore.CommitAll();
                        context.Load(group);
                        context.Load(termStore);
                        context.ExecuteQueryRetry();

                        newGroup = true;
                    }
                }
            }
            #endregion

            session.UpdateCache();
            session.Context.ExecuteQueryRetry();
            #region TermSets

            foreach (var modelTermSet in modelTermGroup.TermSets)
            {
                TermSet set        = null;
                var     newTermSet = false;

                var normalizedTermSetName = TaxonomyItem.NormalizeName(context, parser.ParseString(modelTermSet.Name));
                context.ExecuteQueryRetry();
                if (!newGroup)
                {
                    set =
                        group.TermSets.FirstOrDefault(
                            ts => ts.Id == modelTermSet.Id || ts.Name == normalizedTermSetName.Value);
                }
                if (set == null)
                {
                    if (modelTermSet.Id == Guid.Empty)
                    {
                        modelTermSet.Id = Guid.NewGuid();
                    }
                    else
                    {
                        if (CheckIfTermSetIdIsUnique(termStore, modelTermSet.Id) == false)
                        {
                            throw new Exception($"Termset ID {modelTermSet.Id} is already present in termstore");
                        }
                    }
                    var termSetLanguage = modelTermSet.Language.HasValue ? modelTermSet.Language.Value : termStore.DefaultLanguage;
                    set = group.CreateTermSet(normalizedTermSetName.Value, modelTermSet.Id, termSetLanguage);
                    parser.AddToken(new TermSetIdToken(context.Web, group.Name, normalizedTermSetName.Value, modelTermSet.Id));
                    if (siteCollectionTermGroup != null && !siteCollectionTermGroup.ServerObjectIsNull.Value)
                    {
                        if (group.Name == siteCollectionTermGroup.Name)
                        {
                            parser.AddToken((new SiteCollectionTermSetIdToken(context.Web, normalizedTermSetName.Value, modelTermSet.Id)));
                        }
                    }
                    newTermSet = true;
                    if (!string.IsNullOrEmpty(modelTermSet.Description))
                    {
                        set.Description = parser.ParseString(modelTermSet.Description);
                    }
                    set.IsOpenForTermCreation = modelTermSet.IsOpenForTermCreation;
                    set.IsAvailableForTagging = modelTermSet.IsAvailableForTagging;
                    foreach (var property in modelTermSet.Properties)
                    {
                        set.SetCustomProperty(property.Key, parser.ParseString(property.Value));
                    }
                    if (modelTermSet.Owner != null)
                    {
                        set.Owner = parser.ParseString(modelTermSet.Owner);
                    }
                    termStore.CommitAll();
                    context.Load(set);
                    context.ExecuteQueryRetry();
                }

                context.Load(set, s => s.Terms.Include(t => t.Id, t => t.Name));
                context.ExecuteQueryRetry();
                var terms = set.Terms;

                foreach (var modelTerm in modelTermSet.Terms)
                {
                    if (!newTermSet)
                    {
                        if (terms.Any())
                        {
                            var term = terms.FirstOrDefault(t => t.Id == modelTerm.Id);
                            if (term == null)
                            {
                                var normalizedTermName = TaxonomyItem.NormalizeName(context, parser.ParseString(modelTerm.Name));
                                context.ExecuteQueryRetry();

                                term = terms.FirstOrDefault(t => t.Name == normalizedTermName.Value);
                                if (term == null)
                                {
                                    var returnTuple = CreateTerm(context, modelTerm, set, termStore, parser, scope);
                                    if (returnTuple != null)
                                    {
                                        modelTerm.Id = returnTuple.Item1;
                                        parser       = returnTuple.Item2;
                                    }
                                    reusedTerms.AddRange(returnTuple.Item3);
                                }
                                else
                                {
                                    // todo: add handling for reused term?
                                    modelTerm.Id = term.Id;
                                }
                            }
                            else
                            {
                                // todo: add handling for reused term?
                                modelTerm.Id = term.Id;
                            }

                            if (term != null)
                            {
                                CheckChildTerms(context, modelTerm, term, termStore, parser, scope);
                            }
                        }
                        else
                        {
                            var returnTuple = CreateTerm(context, modelTerm, set, termStore, parser, scope);
                            if (returnTuple != null)
                            {
                                modelTerm.Id = returnTuple.Item1;
                                parser       = returnTuple.Item2;
                            }
                            reusedTerms.AddRange(returnTuple.Item3);
                        }
                    }
                    else
                    {
                        var returnTuple = CreateTerm(context, modelTerm, set, termStore, parser, scope);
                        if (returnTuple != null)
                        {
                            modelTerm.Id = returnTuple.Item1;
                            parser       = returnTuple.Item2;
                        }
                        reusedTerms.AddRange(returnTuple.Item3);
                    }
                }

                // do we need custom sorting?
                if (modelTermSet.Terms.Any(t => t.CustomSortOrder > 0))
                {
                    var sortedTerms = modelTermSet.Terms.OrderBy(t => t.CustomSortOrder);

                    var customSortString = sortedTerms.Aggregate(string.Empty,
                                                                 (a, i) => a + i.Id.ToString() + ":");
                    customSortString = customSortString.TrimEnd(new[] { ':' });

                    set.CustomSortOrder = customSortString;
                    termStore.CommitAll();
                    context.ExecuteQueryRetry();
                }
            }

            #endregion

            return(reusedTerms);
        }