예제 #1
0
        public IEnumerable <SPTerm> GetTerms(String setName)
        {
            const Int32 englishLCID = 1033;

            var currentLCID = CultureInfo.CurrentCulture.LCID;

            var terms = new List <SPTerm>();

            var sortOrders = new SortOrdersCollection <Guid>();

            var session = TaxonomySession.GetTaxonomySession(_context);

            var termSets = session.GetTermSetsByName(setName, englishLCID);

            _context.Load(session, s => s.TermStores);
            _context.Load(termSets);

            _context.ExecuteQuery();

            var termSet = termSets.First();

            var allTerms = termSet.GetAllTerms();

            _context.Load(allTerms);

            _context.ExecuteQuery();

            var guids = allTerms.ToList().Select(t => t.Id).ToList();

            Parallel.ForEach(guids, CreateClientContext, (id, state, context) =>
            {
                try
                {
                    GetTermItem(setName, context, englishLCID, id, currentLCID, terms, sortOrders);
                }
                catch (WebException)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    GetTermItem(setName, context, englishLCID, id, currentLCID, terms, sortOrders);
                }

                return(context);
            }
                             , context => context.Dispose());

            var customSortOrder = sortOrders.GetSortOrders();

            terms.Sort(new SPTermComparerByCustomSortOrder(customSortOrder));

            if (terms.Count == 0)
            {
                throw new Exception("No terms was readed!");
            }

            return(terms);
        }
예제 #2
0
        private static void GetTermItem(String setName, ClientContext context, Int32 englishLCID, Guid id, Int32 currentLCID,
                                        List <SPTerm> terms, SortOrdersCollection <Guid> sortOrders)
        {
            var lsession = TaxonomySession.GetTaxonomySession(context);

            var ltermSets = lsession.GetTermSetsByName(setName, englishLCID);

            context.Load(lsession, s => s.TermStores);
            context.Load(ltermSets);
            context.ExecuteQuery();

            var ltermSet = ltermSets.First();

            var term = ltermSet.GetTerm(id);

            context.Load(term);

            context.ExecuteQuery();

            var name = term.Name;

            var spTerm = new SPTerm
            {
                Id         = term.Id,
                Name       = name,
                Path       = term.PathOfTerm,
                Properties = term.LocalCustomProperties
            };

            if (currentLCID != englishLCID) // If lcid label not avaliable or lcid==1033 keep default label
            {
                var langLabel = term.GetAllLabels(currentLCID);

                context.Load(langLabel);
                context.ExecuteQuery();

                if (langLabel.Count != 0)
                {
                    spTerm.Name = langLabel.First().Value;
                }
            }

            terms.Add(spTerm);

            if (!String.IsNullOrEmpty(term.CustomSortOrder))
            {
                sortOrders.AddSortOrder(term.CustomSortOrder);
            }
        }
예제 #3
0
        private static void GetTermItem(String setName, ClientContext context, Int32 englishLCID, Guid id, Int32 currentLCID,
            List<SPTerm> terms, SortOrdersCollection<Guid> sortOrders)
        {
            var lsession = TaxonomySession.GetTaxonomySession(context);

            var ltermSets = lsession.GetTermSetsByName(setName, englishLCID);

            context.Load(lsession, s => s.TermStores);
            context.Load(ltermSets);
            context.ExecuteQuery();

            var ltermSet = ltermSets.First();

            var term = ltermSet.GetTerm(id);

            context.Load(term);

            context.ExecuteQuery();

            var name = term.Name;

            var spTerm = new SPTerm
            {
                Id = term.Id,
                Name = name,
                Path = term.PathOfTerm,
                Properties = term.LocalCustomProperties
            };

            if (currentLCID != englishLCID) // If lcid label not avaliable or lcid==1033 keep default label
            {
                var langLabel = term.GetAllLabels(currentLCID);

                context.Load(langLabel);
                context.ExecuteQuery();

                if (langLabel.Count != 0)
                {
                    spTerm.Name = langLabel.First().Value;
                }
            }

            terms.Add(spTerm);

            if (!String.IsNullOrEmpty(term.CustomSortOrder))
            {
                sortOrders.AddSortOrder(term.CustomSortOrder);
            }
        }
예제 #4
0
        public IEnumerable<SPTerm> GetTerms(String setName)
        {
            const Int32 englishLCID = 1033;

            var currentLCID = CultureInfo.CurrentCulture.LCID;

            var terms = new List<SPTerm>();

            var sortOrders = new SortOrdersCollection<Guid>();

            var session = TaxonomySession.GetTaxonomySession(_context);

            var termSets = session.GetTermSetsByName(setName, englishLCID);

            _context.Load(session, s=>s.TermStores);
            _context.Load(termSets);

            _context.ExecuteQuery();

            var termSet = termSets.First();

            var allTerms = termSet.GetAllTerms();

            _context.Load(allTerms);

            _context.ExecuteQuery();

            var guids = allTerms.ToList().Select(t => t.Id).ToList();

            Parallel.ForEach(guids, CreateClientContext, (id, state, context) =>
            {
                try
                {
                    GetTermItem(setName, context, englishLCID, id, currentLCID, terms, sortOrders);
                }
                catch (WebException)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    GetTermItem(setName, context, englishLCID, id, currentLCID, terms, sortOrders);
                }

                return context;
            }
            , context => context.Dispose());

            var customSortOrder = sortOrders.GetSortOrders();

            terms.Sort(new SPTermComparerByCustomSortOrder(customSortOrder));

            if (terms.Count == 0) throw new Exception("No terms was readed!");

            return terms;
        }