TaxonomyWebTaggingControl – Sharepoint

Hi guys , in this article i will tell you how to use TaxonomyWebTaggingControl . First of all i assume that you already define your terms and terms sets in your sharepoint managed meta data service.This control help us that show and set this terms for various purpose. Msdn says “A TaxonomyWebTaggingControl object is a Web control that can be added to a page so that a user can pick one or more taxonomy Term object from a specific TermSet object or from an entire TermStore object.”

First we register control in our page definition.

<%@ Register Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.Taxonomy" TagPrefix="cc1" %>

And control in the content like this

<cc1:TaxonomyWebTaggingControl ID="TaxonomyControl" runat="server" >
</cc1:TaxonomyWebTaggingControl>

At code behind we getting taxonomy store from metadataservice and set the control properties

Dont forget to add your libraries.

using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;
And your code here
SPContext context = SPContext.Current;
SPSite site = context.Site;
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["Managed Metadata Service"]; //if you a custom meta service you should change name.
Group group = termStore.Groups["MyGroup"];
TermSet productsTermSet = group.TermSets["Genders"];

.the code above just getting metadata objects from service via using taxonomy entities.

In my code i am usign UserProfileService which has a user property named “Gender” means “Male,Female” which is defined in metadataservice. I am going to set TaxonomyWebTaggingControl (ID: TaxonomyControl) and get value from this control.

Bind the control with entities

TaxonomyControl.SspId.Add(termStore.Id);

TaxonomyControl.TermSetId.Add(productsTermSet.Id);

TaxonomyControl.IsAddTerms = true;

TaxonomyControl.AllowFillIn = true;

TaxonomyControl.IsMulti = false;

Setting data to control with userprofile object:


string
termName = service.GetUserPropertyByAccountName(loginName, ProfileProperties.Genders).GetSingleValue();// don’t worry about this line this is not in our concept.Just know this line getting value from UserProfileService as string.

TaxonomyControl.Text = string.Format(“{0}|{1}”, termName, productsTermSet.Terms[termName].Id.ToString());

TaxonomyWebTaggingControl has a propery named “Text” . You should set this property  “[Label] | [Guid]” format.
Set values into this control as pairs of labels and GUIDs. The delimiter between a label and a GUID is the | character. When using multiple values, delimit them with a ; character. Example: term1|12345678-1234-1234-1234-123456789012;term2|87654321-4321-4321-4321-210987654321

Getting data from control :

TaxonomyFieldValueCollection portfolioCountriesValues = new TaxonomyFieldValueCollection(String.Empty);

portfolioCountriesValues.PopulateFromLabelGuidPairs(TaxonomyControl.Text);

profile[ProfileProperties.Genders].Value=  portfolioCountriesValues[0].Label;

We are using TaxonomyFieldValueCollection class that help us parsing data.

For More info:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomywebtaggingcontrol.aspx

Happy codding…