Overview
TaxonomyWebTaggingControl represents a generic Web control for picking managed metadata terms.
During initialization two properties are mandatory to bind this control with Managed Metadata Service (MMS):
Below is presented example how to initialize it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Initializing TaxonomyWebTaggingControl control | |
/// </summary> | |
/// <param name="countryControl"> </param> | |
/// <param name="session"></param> | |
/// <param name="countriesTermSet"></param> | |
private void InitCountriesControl(TaxonomyWebTaggingControl countryControl, TaxonomySession session, TermSet countriesTermSet) | |
{ | |
countryControl.SSPList = countriesTermSet.TermStore.Id.ToString(); | |
countryControl.TermSetList = countriesTermSet.Id.ToString(); | |
} |
Country picker
Let us consider the case when not all terms for the term set should be shown but a subset only. Assume we have the following structure for the countries:
- the first level is the continents
- the second level is the countries
and our goal to display countries in TaxonomyWebTaggingControl only for selected continent as shown below
This could be achieved by setting AnchorId property for TaxonomyWebTaggingControl control that allows to specify ID of parent Term for any valid value in control.
Implementation
Country picker allows to select continent/country from Countries term set.
Depending on the selected continent only countries specific for continent are shown in TaxonomyWebTaggingControl control
The code below contains Country Picker implementation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Pingback: Using SharePoint TaxonomyWebTaggingControl control: Access and manipulate from client side | Yet Another SharePoint Blog