I have created a custom list which will have the following columns.
Taxonomy1 metadata field is mapped to the following termset.
I have created a farm solution in which I have added a custom application page. In this application page I have added the taxonomy field.
In the application page (ApplicationPage.aspx) please add the following highlighted code.
- <%@ Register TagPrefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
- </asp:Content>
- <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <p>
- Taxonomy1</p>
- </td>
- <td>
- <Taxonomy:TaxonomyWebTaggingControl runat="server" ID="taxCtrl1" Visible="true" IsDisplayPickerButton="true"
- IsMulti="true" AllowFillIn="true" IsAddTerms="true">
- </Taxonomy:TaxonomyWebTaggingControl>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <asp:Button ID="btnGet" runat="server" OnClick="btnGet_Click" Text="Get Taxonomy Values" />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <asp:Label ID="lbl" runat="server"></asp:Label>
- </td>
- </tr>
- </table>
- </div>
- </asp:Content>
- <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
- Application Page
- </asp:Content>
- <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
- runat="server">
- My Application Page
- </asp:Content>
In the code behind file add the following code.
- protected void Page_Load(object sender, EventArgs e)
- {
- GetTaxonomyFieldTermset("Taxonomy", "Taxonomy1", this.taxCtrl1);
- }
-
- private void GetTaxonomyFieldTermset(string listName, string fieldName, TaxonomyWebTaggingControl taxCtrl)
- {
- SPList list = SPContext.Current.Web.Lists.TryGetList(listName);
- if (list != null)
- {
- TaxonomyField taxonomyField = list.Fields[fieldName] as TaxonomyField;
- if (taxonomyField != null)
- {
- taxCtrl.SspId.Add(taxonomyField.SspId);
- taxCtrl.TermSetId.Add(taxonomyField.TermSetId);
- }
- }
- }
-
- protected void btnGet_Click(object sender, EventArgs e)
- { TaxonomyFieldValueCollection taxFieldValColl=TaxonomyFieldControl.GetTaxonomyCollection(this.taxCtrl1.Text);
- foreach (TaxonomyFieldValue taxFieldVal in taxFieldValColl)
- {
- lbl.Text += taxFieldVal.Label;
- }
- }
Deploy the solution and navigate to the custom page. You could be able to see the taxonomy field control in the custom application page. Select the term for the taxonomy field and then click on the button. Selected term is displayed as shown below.
Thus in this blog you have seen how to add a taxonomy field control in a custom SharePoint page.