How to add a taxonomy field control in a custom page

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.

  1. <%@ Register TagPrefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>    
  2. <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">    
  3. </asp:Content>    
  4. <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">    
  5.     <div>    
  6.         <table>    
  7.             <tr>    
  8.                 <td>    
  9.                     <p>    
  10.                         Taxonomy1</p>    
  11.                 </td>    
  12.                 <td>    
  13.                     <Taxonomy:TaxonomyWebTaggingControl runat="server" ID="taxCtrl1" Visible="true" IsDisplayPickerButton="true"    
  14.                         IsMulti="true" AllowFillIn="true" IsAddTerms="true">    
  15.                     </Taxonomy:TaxonomyWebTaggingControl>    
  16.                 </td>    
  17.             </tr>    
  18.             <tr>    
  19.                 <td>    
  20.                 </td>    
  21.                 <td>    
  22.                     <asp:Button ID="btnGet" runat="server" OnClick="btnGet_Click" Text="Get Taxonomy Values" />    
  23.                 </td>    
  24.             </tr>    
  25.             <tr>    
  26.                 <td>    
  27.                 </td>    
  28.                 <td>    
  29.                     <asp:Label ID="lbl" runat="server"></asp:Label>    
  30.                 </td>    
  31.             </tr>    
  32.         </table>    
  33.     </div>    
  34. </asp:Content>    
  35. <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">    
  36.     Application Page    
  37. </asp:Content>    
  38. <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"    
  39.     runat="server">    
  40.     My Application Page    
  41. </asp:Content>   

 In the code behind file add the following code.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     GetTaxonomyFieldTermset("Taxonomy""Taxonomy1"this.taxCtrl1);  
  4. }  
  5.   
  6. private void GetTaxonomyFieldTermset(string listName, string fieldName, TaxonomyWebTaggingControl taxCtrl)  
  7. {  
  8.     SPList list = SPContext.Current.Web.Lists.TryGetList(listName);  
  9.     if (list != null)  
  10.     {  
  11.         TaxonomyField taxonomyField = list.Fields[fieldName] as TaxonomyField;  
  12.         if (taxonomyField != null)  
  13.         {  
  14.             taxCtrl.SspId.Add(taxonomyField.SspId);  
  15.             taxCtrl.TermSetId.Add(taxonomyField.TermSetId);  
  16.         }  
  17.     }  
  18. }  
  19.   
  20. protected void btnGet_Click(object sender, EventArgs e)  
  21. {                             TaxonomyFieldValueCollection taxFieldValColl=TaxonomyFieldControl.GetTaxonomyCollection(this.taxCtrl1.Text);  
  22.    foreach (TaxonomyFieldValue taxFieldVal in taxFieldValColl)  
  23.    {  
  24.       lbl.Text += taxFieldVal.Label;  
  25.    }  
  26. }  

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.