Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Picker for Countries Term Set
public class CountryPickerPage : LayoutsPageBase
{
protected override void OnInit(EventArgs e)
{
continentsControl.AutoPostBack = true;
continentsControl.SelectedIndexChanged += ContinentsControlChanged;
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
var session = new TaxonomySession(SPContext.Current.Site, false);
if(!Page.IsPostBack)
{
var countriesTermSet = InitContinentsControl(session);
InitCountriesControl(session, countriesTermSet,null);
}
base.OnLoad(e);
}
private TermSet InitContinentsControl(TaxonomySession session)
{
var termStore = session.DefaultKeywordsTermStore;
var groupSE = termStore.Groups["Geography"];
var countriesTermSet = groupSE.TermSets["Countries"];
if (countriesTermSet != null)
{
var countriesTerms = countriesTermSet.Terms;
continentsControl.DataTextField = "Name";
continentsControl.DataValueField = "Id";
continentsControl.DataSource = countriesTerms;
continentsControl.DataBind();
}
return countriesTermSet;
}
private void InitCountriesControl(TaxonomySession session, TermSet countriesTermSet, Term continentTerm)
{
if (countriesTermSet != null)
{
this.countryControl.SSPList = countriesTermSet.TermStore.Id.ToString();
this.countryControl.TermSetList = countriesTermSet.Id.ToString();
}
else if (continentTerm != null)
{
this.countryControl.SSPList = continentTerm.TermStore.Id.ToString();
this.countryControl.AnchorId = continentTerm.Id;
this.countryControl.TermSetList = continentTerm.TermSet.Id.ToString();
}
}
void ContinentsControlChanged(object sender, EventArgs e)
{
var control = sender as DropDownList;
if (control != null)
{
var continentTermId = new Guid(control.SelectedValue);
var session = new TaxonomySession(SPContext.Current.Site, false);
var termStore = session.DefaultKeywordsTermStore;
var continentTerm = termStore.GetTerm(continentTermId);
InitCountriesControl(session, null, continentTerm);
}
}
protected TaxonomyWebTaggingControl countryControl;
protected DropDownList continentsControl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment