public static string DeleteTerm(ClientContext clientContext, TermQueryModel model)
        {
            var pickerTermSet = new PickerTermSetModel();

            //var searchString = (string)HttpContext.Current.Request["SearchString"];

            if (clientContext != null)
            {
                //Get terms from the 'Keywords' termset for autocomplete suggestions.
                // It might be a good idea to cache these values.

                var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
                var termStore       = taxonomySession.GetDefaultKeywordsTermStore();
                var termToDelete    = termStore.GetTerm(new Guid(model.Id));

                clientContext.Load(termToDelete);
                clientContext.ExecuteQuery();

                termToDelete.DeleteObject();
                clientContext.ExecuteQuery();

                var termSetId = new Guid(model.TermSetId);
                var termSet   = termStore.GetTermSet(termSetId);

                clientContext.Load(termSet, ts => ts.Id, ts => ts.IsOpenForTermCreation, ts => ts.CustomSortOrder, ts => ts.Name,
                                   ts => ts.Terms.Include(t => t.PathOfTerm,
                                                          t => t.Id,
                                                          t => t.Labels.Include(l => l.IsDefaultForLanguage, l => l.Value),
                                                          t => t.Name,
                                                          t => t.TermsCount));
                clientContext.ExecuteQuery();

                pickerTermSet.Id   = termSet.Id.ToString().Replace("{", string.Empty).Replace("}", string.Empty);
                pickerTermSet.Name = termSet.Name;
                pickerTermSet.IsOpenForTermCreation = termSet.IsOpenForTermCreation;
                pickerTermSet.CustomSortOrder       = termSet.CustomSortOrder;
                pickerTermSet.Terms = new List <PickerTermModel>();

                foreach (var term in termSet.Terms.ToList <Term>())
                {
                    pickerTermSet.Terms.Add(GetPickerTerm(term));
                }
            }
            return(JsonHelper.Serialize <PickerTermSetModel>(pickerTermSet));
        }
        public static string GetTaxonomyPickerData(ClientContext clientContext, TermSetQueryModel model)
        {
            var pickerTermSet = new PickerTermSetModel();

            if (clientContext != null)
            {
                //Get terms from the 'Keywords' termset for autocomplete suggestions.
                // It might be a good idea to cache these values.

                var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
                var termStore       = taxonomySession.GetDefaultSiteCollectionTermStore();

                TermSet termSet = null;

                if (!string.IsNullOrWhiteSpace(model.Id))
                {
                    termSet = termStore.GetTermSet(new Guid(model.Id));
                }
                else if (!string.IsNullOrWhiteSpace(model.Name))
                {
                    var rawTermSets = termStore.GetTermSetsByName(model.Name, model.LCID);
                    termSet = rawTermSets.GetByName(model.Name);
                }
                else if (model.UseHashtags)
                {
                    termSet = termStore.HashTagsTermSet;
                }
                else if (model.UseKeywords)
                {
                    termSet = termStore.KeywordsTermSet;
                }

                clientContext.Load(termSet, ts => ts.Id, ts => ts.IsOpenForTermCreation, ts => ts.CustomSortOrder, ts => ts.Name,
                                   ts => ts.Terms.Include(t => t.PathOfTerm,
                                                          t => t.Id,
                                                          t => t.Labels.Include(l => l.IsDefaultForLanguage, l => l.Value),
                                                          t => t.Name,
                                                          t => t.TermsCount));
                clientContext.ExecuteQuery();

                var allTerms = termSet.GetAllTerms();

                clientContext.Load(allTerms, terms => terms.Include(t => t.PathOfTerm,
                                                                    t => t.Id,
                                                                    t => t.Labels.Include(l => l.IsDefaultForLanguage, l => l.Value),
                                                                    t => t.Name,
                                                                    t => t.TermsCount));
                clientContext.ExecuteQuery();

                pickerTermSet.Id   = termSet.Id.ToString().Replace("{", string.Empty).Replace("}", string.Empty);
                pickerTermSet.Name = termSet.Name;
                pickerTermSet.IsOpenForTermCreation = termSet.IsOpenForTermCreation;
                pickerTermSet.CustomSortOrder       = termSet.CustomSortOrder;
                pickerTermSet.Terms     = new List <PickerTermModel>();
                pickerTermSet.FlatTerms = new List <PickerTermModel>();

                foreach (var term in termSet.Terms.ToList <Term>())
                {
                    pickerTermSet.Terms.Add(GetPickerTerm(term));
                }

                foreach (var term in allTerms.ToList <Term>())
                {
                    pickerTermSet.FlatTerms.Add(GetPickerTerm(term, false));
                }
            }
            return(JsonHelper.Serialize <PickerTermSetModel>(pickerTermSet));
        }