Esempio n. 1
0
        public static string GetTermIdForTerm(string term, Guid termSetId, ClientContext clientContext)
        {
            string termId = string.Empty;

            //Get term set from ID
            TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore       ts       = tSession.GetDefaultSiteCollectionTermStore();
            TermSet         tset     = ts.GetTermSet(termSetId);

            LabelMatchInformation lmi = new LabelMatchInformation(clientContext);

            lmi.Lcid            = 1033;
            lmi.TrimUnavailable = true;
            lmi.TermLabel       = term;

            //Search for matching terms in the term set based on label
            TermCollection termMatches = tset.GetTerms(lmi);

            clientContext.Load(tSession);
            clientContext.Load(ts);
            clientContext.Load(tset);
            clientContext.Load(termMatches);

            clientContext.ExecuteQuery();

            //Set term ID to first match
            if (termMatches != null && termMatches.Count() > 0)
            {
                termId = termMatches.First().Id.ToString();
            }

            return(termId);
        }
Esempio n. 2
0
        private string GetTermIdForTaxonomyField(TaxonomyField field, string term, ListItem pendingItem, Microsoft.SharePoint.Client.File pendingFile)
        {
            if (_terms == null)
            {
                _terms = new Dictionary <string, IDictionary <string, string> >();
            }

            if (!_terms.Keys.Contains(field.Title))
            {
                _terms[field.Title] = new Dictionary <string, string>();
            }

            if (_terms[field.Title].Keys.Contains(term))
            {
                return(_terms[field.Title][term].ToString());
            }

            var termId = string.Empty;

            //before we go forward,save pending item
            pendingItem.Update();
            ctx.Load(pendingFile);
            ctx.ExecuteQuery();

            TaxonomySession tSession = TaxonomySession.GetTaxonomySession(ctx);

            ctx.Load(tSession.TermStores);
            ctx.ExecuteQuery();
            TermStore ts   = tSession.TermStores.First();
            TermSet   tset = ts.GetTermSet(field.TermSetId);

            LabelMatchInformation lmi = new LabelMatchInformation(ctx);

            lmi.Lcid            = 1033;
            lmi.TrimUnavailable = true;
            lmi.TermLabel       = term;

            TermCollection termMatches = tset.GetTerms(lmi);

            ctx.Load(tSession);
            ctx.Load(ts);
            ctx.Load(tset);
            ctx.Load(termMatches);

            ctx.ExecuteQuery();

            if (termMatches != null && termMatches.Count() > 0)
            {
                termId = termMatches.First().Id.ToString();
            }

            _terms[field.Title][term] = termId;

            return(termId);
        }
Esempio n. 3
0
        public static string GetTermIdForTerm(string term, Guid termSetId, ClientContext clientContext)
        {
            string termId = string.Empty;

            TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore       ts       = tSession.GetDefaultSiteCollectionTermStore();
            TermSet         tset     = ts.GetTermSet(termSetId);

            LabelMatchInformation lmi = new LabelMatchInformation(clientContext);

            lmi.Lcid            = 1033;
            lmi.TrimUnavailable = true;
            lmi.TermLabel       = term;

            TermCollection termMatches = tset.GetTerms(lmi);


            //tset.CreateTerm()
            clientContext.Load(tSession);
            clientContext.Load(ts);
            clientContext.Load(tset);
            clientContext.Load(termMatches);

            clientContext.ExecuteQuery();

            if (termMatches != null && termMatches.Count() > 0)
            {
                termId = termMatches.First().Id.ToString();
            }
            else
            {
                Term TermAdd = tset.CreateTerm(term, 1033, Guid.NewGuid());
                clientContext.ExecuteQuery();
                clientContext.Load(TermAdd);
                clientContext.ExecuteQuery();
                termId = TermAdd.Id.ToString();
            }


            return(termId);
        }