Taxonomy Term Data Binding With Dropdown List In SharePoint App Model

Term Store Manangement

How to access SharePoint Site Term Store Management?

Go to SharePoint Site, then Site Settings and select Term Store Management.

Term Store Management

Term Store Management Concept

Term Store Management Concept

SharePoint APP Model

Step 1: Add Permission in AppManifest.xml file to read Taxonomy as follows.

permissions

Step 2: User Interface Design Page.

Taxonomy.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Texonomy.aspx.cs" Inherits="SP_TexonomyProWeb.Pages.Texonomy" %>    
  2.     
  3. <!DOCTYPE html>    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     <title></title>    
  8. </head>    
  9. <body>    
  10.     <form id="form1" runat="server">    
  11.         <div>    
  12.     
  13.             <table>    
  14.                 <tr>    
  15.     
  16.                     <td>    
  17.                         <asp:Label ID="Label1" runat="server" Text="Region"></asp:Label></td>    
  18.                     <td>    
  19.                         <asp:DropDownList ID="ddlDepartment" runat="server"></asp:DropDownList></td>    
  20.                 </tr>    
  21.                 <tr>    
  22.                     <td colspan="2"> </td>    
  23.                 </tr>    
  24.                 <tr>    
  25.                     <td>    
  26.                         <asp:Label ID="Label2" runat="server" Text="Division"></asp:Label></td>    
  27.                     <td>    
  28.                         <asp:DropDownList ID="ddlJobTitle" runat="server"></asp:DropDownList></td>    
  29.                 </tr>    
  30.             </table>    
  31.     
  32.         </div>    
  33.     </form>    
  34. </body>    
  35. </html>   
Taxonomy.aspx.cs
  1. using Microsoft.SharePoint.Client.Taxonomy;    
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Data;    
  5. using System.Linq;    
  6. using System.Web;    
  7. using System.Web.UI;    
  8. using System.Web.UI.WebControls;    
  9.     
  10. namespace SP_TexonomyProWeb.Pages    
  11. {    
  12.     public partial class Texonomy : System.Web.UI.Page    
  13.     {    
  14.         protected void Page_Load(object sender, EventArgs e)    
  15.         {    
  16.             string TermStoreName = "Managed Metadata Service";    
  17.     
  18.             string TermGroupName = "Texo";    
  19.     
  20.             BindTermstoDropdown(TermStoreName, TermGroupName, "Region", ddlDepartment);    
  21.     
  22.             BindTermstoDropdown(TermStoreName, TermGroupName, "Categories", ddlJobTitle);      
  23.         }    
  24.     
  25.         protected void BindTermstoDropdown(string TermStoreName, string TermGroupName, string TermSetName, DropDownList ddl)    
  26.         {    
  27.             var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);    
  28.     
  29.             using (var clientContext = spContext.CreateUserClientContextForSPHost())    
  30.             {    
  31.                 clientContext.Load(clientContext.Web, web => web.Title);    
  32.                 clientContext.ExecuteQuery();    
  33.                 Response.Write(clientContext.Web.Title);    
  34.     
  35.                 // Get the TaxonomySession    
  36.                 TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);    
  37.     
  38.                 // Get the term store by name    
  39.                 TermStore termStore = taxonomySession.TermStores.GetByName(TermStoreName);    
  40.     
  41.                 // Get the term group by Name    
  42.                 TermGroup termGroup = termStore.Groups.GetByName(TermGroupName);    
  43.     
  44.                 // Get the term set by Name    
  45.                 TermSet termSet = termGroup.TermSets.GetByName(TermSetName);    
  46.     
  47.                 // Get all the terms     
  48.                 TermCollection termColl = termSet.Terms;    
  49.     
  50.                 clientContext.Load(termColl);    
  51.     
  52.                 // Execute the query to the server    
  53.                 clientContext.ExecuteQuery();    
  54.     
  55.                 // Loop through all the terms    
  56.     
  57.                 DataTable dt = new DataTable();    
  58.                 dt.Columns.Add("TermName");    
  59.                 DataRow row = null;    
  60.     
  61.                 foreach (Term term in termColl)    
  62.                 {    
  63.     
  64.                     row = dt.NewRow();    
  65.                     row["TermName"] = term.Name;    
  66.                     dt.Rows.Add(row);    
  67.                 }    
  68.     
  69.                 ddl.DataSource = dt;    
  70.                 ddl.DataTextField = "TermName";    
  71.                 ddl.DataValueField = "TermName";    
  72.                 ddl.DataBind();    
  73.             }    
  74.         }    
  75.     
  76.     }    
  77. }  
Output:

Output

Thank you! If you have any query, then please mention it in the comment section.

 

Next Recommended Readings