public static TermSet LookupTermSet(TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
 {
     return LookupTermSet(tesmStore,
         taxFieldModel.TermSetName,
         taxFieldModel.TermSetId,
         taxFieldModel.TermSetLCID
         );
 }
        public void TaxonomyFieldDefinition_AllowMultipleValues_ShouldUpdate_FieldType()
        {
            var def = new TaxonomyFieldDefinition();

            def.IsMulti = false;
            Assert.IsTrue(def.FieldType == BuiltInFieldTypes.TaxonomyFieldType);

            def.IsMulti = true;
            Assert.IsTrue(def.FieldType == BuiltInFieldTypes.TaxonomyFieldTypeMulti);
        }
        public static Term LookupTerm(TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
        {
            if (taxFieldModel.TermId.HasValue)
                return tesmStore.GetTerm(taxFieldModel.TermId.Value);

            if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                return tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false).FirstOrDefault();

            return null;
        }
        public static TermSet LookupTermSet(SPSite site, TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
        {
            return LookupTermSet(tesmStore,

                taxFieldModel.TermGroupName,
                taxFieldModel.TermGroupId,
                taxFieldModel.IsSiteCollectionGroup,

                site,

                taxFieldModel.TermSetName,
                taxFieldModel.TermSetId,
                taxFieldModel.TermSetLCID
                );
        }
        public static Term LookupTerm(SPSite site, TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
        {
            Group currentGroup = null;

            var termGroupName = taxFieldModel.TermGroupName;
            var groupId = taxFieldModel.TermGroupId;
            var isSiteCollectionGroup = taxFieldModel.IsSiteCollectionGroup;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper());
            }
            else if (groupId != null && groupId.HasGuidValue())
            {
                currentGroup = tesmStore.GetGroup(groupId.Value);
            }
            else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value)
            {
                currentGroup = tesmStore.GetSiteCollectionGroup(site);
            }

            // TODO
            // that should also check if the TermSet is there, so to scope the term 

            if (currentGroup != null)
            {
                if (taxFieldModel.TermId.HasValue)
                    return tesmStore.GetTerm(taxFieldModel.TermId.Value);

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                {
                    return tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false)
                                    .FirstOrDefault(t => t.TermSet.Group.Name == currentGroup.Name);
                }
            }
            else
            {
                if (taxFieldModel.TermId.HasValue)
                    return tesmStore.GetTerm(taxFieldModel.TermId.Value);

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                    return tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false).FirstOrDefault();
            }

            return null;
        }
        public static TermStore LookupTermStore(ClientContext clientContext,
            TaxonomyFieldDefinition taxFieldModel, bool raiseNullRefException)
        {
            var termStore = TaxonomyTermStoreModelHandler.FindTermStore(clientContext.Site,
                                  taxFieldModel.SspName,
                                  taxFieldModel.SspId,
                                  taxFieldModel.UseDefaultSiteCollectionTermStore);

            if (termStore == null && raiseNullRefException)
                throw new ArgumentNullException("termStore is NULL. Please define SspName, SspId or ensure there is a default term store for the giving site.");

            if (termStore != null)
            {
                var storeContext = clientContext;

                storeContext.Load(termStore, s => s.Id);
                storeContext.ExecuteQueryWithTrace();
            }

            return termStore;
        }
 public static TermStore LookupTermStore(SiteModelHost currentSiteModelHost,
     TaxonomyFieldDefinition taxFieldModel)
 {
     return LookupTermStore(currentSiteModelHost, taxFieldModel, false);
 }
 public static TermStore LookupTermStore(ClientContext clientContext,
     TaxonomyFieldDefinition taxFieldModel)
 {
     return LookupTermStore(clientContext, taxFieldModel, false); 
 }
        public static Term LookupTerm(ClientContext clientContext, TermStore termStore,
            TermSet termSet,
            TaxonomyFieldDefinition termModel)
        {
            var context = clientContext;
            var site = clientContext.Site;

            Term result = null;

            TermGroup currenGroup = null;

            var termGroupName = termModel.TermGroupName;
            var termGroupId = termModel.TermGroupId;
            var isSiteCollectionGroup = termModel.IsSiteCollectionGroup;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currenGroup = termStore.Groups.GetByName(termGroupName);

                context.Load(currenGroup);
                context.ExecuteQueryWithTrace();
            }
            else if (termGroupId != null && termGroupId.HasGuidValue())
            {
                currenGroup = termStore.Groups.GetById(termGroupId.Value);

                context.Load(currenGroup);
                context.ExecuteQueryWithTrace();
            }
            else if (isSiteCollectionGroup == true)
            {
                currenGroup = termStore.GetSiteCollectionGroup(site, false);

                context.Load(currenGroup);
                context.ExecuteQueryWithTrace();
            }

            if (currenGroup != null)
            {
                if (termModel.TermId.HasValue)
                {
                    // by ID, the only one match

                    var scope = new ExceptionHandlingScope(context);
                    using (scope.StartScope())
                    {
                        using (scope.StartTry())
                        {
                            result = termStore.GetTerm(termModel.TermId.Value);
                            context.Load(result);
                        }

                        using (scope.StartCatch())
                        {

                        }
                    }

                    context.ExecuteQueryWithTrace();
                }
                else if (!string.IsNullOrEmpty(termModel.TermName))
                {
                    var terms = termStore.GetTerms(new LabelMatchInformation(context)
                    {
                        Lcid = termModel.TermLCID,
                        TermLabel = termModel.TermName,
                        TrimUnavailable = false
                    });

                    context.Load(terms, t => t.Include(
                                                i => i.Id,
                                                i => i.Name,
                                                i => i.TermSet,
                                                i => i.TermSet.Group,
                                                i => i.TermSet.Group.Name
                                                ));
                    context.ExecuteQueryWithTrace();

                    result = terms.FirstOrDefault(t => t.TermSet.Group.Name == currenGroup.Name);

                    if ( (result == null) && (termSet != null ))
                        // sometimes label match information does not return the term 
                    {
                        var allTerms = termSet.GetAllTerms();
                        context.Load(allTerms, t => t.Include(
                                                    i => i.Id,
                                                    i => i.Name,
                                                    i => i.TermSet,
                                                    i => i.TermSet.Group,
                                                    i => i.TermSet.Group.Name,
                                                    i => i.Labels
                                                    ));
                        context.ExecuteQueryWithTrace();

                        result = allTerms.FirstOrDefault(t => (t.TermSet.Group.Name == currenGroup.Name) && (t.Labels.Any(l=>l.Value == termModel.TermName && l.Language == termModel.TermLCID)));
                    }
                }
            }

            else
            {

                if (termModel.TermId.HasValue)
                {
                    var scope = new ExceptionHandlingScope(context);
                    using (scope.StartScope())
                    {
                        using (scope.StartTry())
                        {
                            result = termStore.GetTerm(termModel.TermId.Value);
                            context.Load(result);
                        }

                        using (scope.StartCatch())
                        {

                        }
                    }

                    context.ExecuteQueryWithTrace();
                }
                else if (!string.IsNullOrEmpty(termModel.TermName))
                {
                    var terms = termStore.GetTerms(new LabelMatchInformation(context)
                    {
                        Lcid = termModel.TermLCID,
                        TermLabel = termModel.TermName,
                        TrimUnavailable = false
                    });

                    context.Load(terms);
                    context.ExecuteQueryWithTrace();

                    result = terms.FirstOrDefault();

                    if ((result == null) && (termSet != null))
                        // sometimes label match information does not return the termset 
                    {
                        var allTerms = termSet.GetAllTerms();
                        context.Load(allTerms, t => t.Include(
                            i => i.Id,
                            i => i.Name,
                            i => i.TermSet,
                            i => i.TermSet.Group,
                            i => i.TermSet.Group.Name,
                            i => i.Labels
                            ));
                        context.ExecuteQueryWithTrace();

                        result =
                            allTerms.FirstOrDefault(
                                t => (t.Labels.Any(l=>l.Value == termModel.TermName && l.Language == termModel.TermLCID)));

                    }

                }
                }

            if (result != null && result.ServerObjectIsNull == false)
            {
                context.Load(result);
                context.ExecuteQueryWithTrace();

                return result;
            }

            return null;
        }
 public static Term LookupTerm(SiteModelHost currentSiteModelHost, TermStore termStore,
     TermSet termSet,
     TaxonomyFieldDefinition termModel)
 {
     return LookupTerm(currentSiteModelHost.HostClientContext, termStore, termSet, termModel);
 }
 public static TermSet LookupTermSet(ClientRuntimeContext context, Site site,
     TermStore termStore, TaxonomyFieldDefinition taxFieldModel)
 {
     return LookupTermSet(context, termStore, site,
         taxFieldModel.TermGroupName, taxFieldModel.TermGroupId, taxFieldModel.IsSiteCollectionGroup,
         taxFieldModel.TermSetName, taxFieldModel.TermSetId, taxFieldModel.TermSetLCID);
 }
        public static TermSet LookupTermSet(
            SiteModelHost currentSiteModelHost,
            TermStore termStore,
            TaxonomyFieldDefinition taxFieldModel)
        {
            var storeContext = currentSiteModelHost.HostClientContext;

            return LookupTermSet(storeContext, currentSiteModelHost.HostSite, termStore, taxFieldModel);
        }
        public static TermSet LookupTermSet(
          ClientContext clientContext,
          TermStore termStore,
          TaxonomyFieldDefinition taxFieldModel)
        {

            return LookupTermSet(clientContext, clientContext.Site, termStore, taxFieldModel);
        }
        public static TermStore LookupTermStore(SPSite site, TaxonomyFieldDefinition taxFieldModel)
        {
            var taxSession = new TaxonomySession(site);

            if (taxFieldModel.UseDefaultSiteCollectionTermStore == true)
                return taxSession.DefaultSiteCollectionTermStore;

            if (taxFieldModel.SspId.HasValue)
                return taxSession.TermStores[taxFieldModel.SspId.Value];

            if (!string.IsNullOrEmpty(taxFieldModel.SspName))
                return taxSession.TermStores[taxFieldModel.SspName];

            return null;
        }
        public static Term LookupTerm(SiteModelHost currentSiteModelHost, TermStore termStore, TaxonomyFieldDefinition termModel)
        {
            var context = currentSiteModelHost.HostClientContext;
            Term result = null;

            if (termModel.TermId.HasValue)
            {
                var scope = new ExceptionHandlingScope(context);
                using (scope.StartScope())
                {
                    using (scope.StartTry())
                    {
                        result = termStore.GetTerm(termModel.TermId.Value);
                        context.Load(result);
                    }

                    using (scope.StartCatch())
                    {

                    }
                }

                context.ExecuteQueryWithTrace();
            }
            else if (!string.IsNullOrEmpty(termModel.TermName))
            {
                var terms = termStore.GetTerms(new LabelMatchInformation(context)
                {
                    Lcid = termModel.TermLCID,
                    TermLabel = termModel.TermName,
                    TrimUnavailable = false
                });

                context.Load(terms);
                context.ExecuteQueryWithTrace();

                result = terms.FirstOrDefault();
            }

            if (result != null && result.ServerObjectIsNull == false)
            {
                context.Load(result);
                context.ExecuteQueryWithTrace();

                return result;
            }

            return null;
        }
 public static TermSet LookupTermSet(ClientRuntimeContext context, TermStore termStore, TaxonomyFieldDefinition taxFieldModel)
 {
     return LookupTermSet(context, termStore,
         taxFieldModel.TermSetName, taxFieldModel.TermSetId, taxFieldModel.TermSetLCID);
 }