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…

About these ads

About bpostaci
Support Engineer in Microsoft.

One Response to TaxonomyWebTaggingControl – Sharepoint

  1. Leandro says:

    Hi Bugra, this article was very useful for me, including this control in a Visual Web Part. But I had one problem. I define several term set as follow (for example):
    TermSet TermSet1 = group.TermSets["Set1"];
    TermSet TermSet2 = group.TermSets["Set2"];
    TermSet TermSet3 = group.TermSets["Set3"];
    TermSet TermSet4 = group.TermSets["Set4"];
    and after that, I add them to the control:
    TaxonomyControl.TermSetId.Add(productsTermSet1.Id);
    TaxonomyControl.TermSetId.Add(productsTermSet2.Id)….
    But when I deploy, there are not any term set on the control. This issue does not happen if I only define one and add one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 40 other followers

%d bloggers like this: