6

I have a custom custom sharepoint form which i have created using visual webpart.

I want to have a Metadata field in my custom form.However, i am not able to find it in my ascx file.

Please help.

  • What you mean by have metadata field? You want the taxonomy selector control? – Arsalan Adam Khatri Feb 17 '14 at 7:02
10

a- Add reference to "Microsoft.SharePoint.Taxonomy" assembly

b- You need to register following Assembly in your Web Part

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

c- You can use the control as following

<Taxonomy:TaxonomyWebTaggingControl runat="server" ID="TaxonomyWebTaggingControl1"
   Visible="true" 
   IsDisplayPickerButton="true" <!--this will show the Picker Button near the textbox.-->
   IsMulti="true" <!--this will allow user to select Single or Multiple Terms.-->
   AllowFillIn="true" <!-- Allows user to add new terms.-->
   IsAddTerms="false" <!-- control will try to add unresolved Term objects to the TermSet object -->
   IsIncludePathData="false"> <!-- Includes Full Path of Term (Id of Group, TermSet & Term Itself) -->
</Taxonomy:TaxonomyWebTaggingControl>

d- Now assign control to Managed Metadata.. You can use following code in PageLoad of Web Part:

TaxonomySession taxonomySession = new TaxonomySession(SPContext.Current.Site);
TaxonomyWebTaggingControl1.SspId.Add(taxonomySession.TermStores["Managed Metadata Service"].Id);
TaxonomyWebTaggingControl1.SSPList = taxonomySession.TermStores["Managed Metadata Service"].Id.ToString();
TaxonomyWebTaggingControl1.TermSetId.Add(taxonomySession.TermStores["Managed Metadata Service"].Groups["tGroup"].TermSets["tTermSet"].Id);
TaxonomyWebTaggingControl1.TermSetList = taxonomySession.TermStores["Managed Metadata Service"].Groups["tGroup"].TermSets["tTermSet"].Id.ToString();
  • Use your own Group and Term Sets here

e- Get the values in code behind:

// this will contain the error message if the values are not correct.
string validationMessage;
var valid = TaxonomyWebTaggingControl1.Validate(out validationMessage);

if (valid)
{
   var values = new TaxonomyFieldValueCollection(string.Empty);
   values.PopulateFromLabelGuidPairs(TaxonomyWebTaggingControl1.Text);
   foreach (TaxonomyFieldValue value in values)
   {
      var wssId = value.WssId; 
      var termGuid = value.TermGuid;
      var termlabel = value.Label
   }
}
else
{ 
   // display validation error message 
}

References:
Add Taxonomy Field / TaxonomyWebTaggingControl to the Web part or Application Page
Build Your Own User Interface Components Using The Taxonomy Controls

  • excellent explanation! +1 – Mike Apr 10 '14 at 14:31
  • Good answer! Might be a good idea to add "e", how to get value from control after selection. – George Norberg Apr 14 '15 at 12:38
  • 1
    @GeorgeNorberg added "e" :) – Arsalan Adam Khatri Apr 14 '15 at 13:36
  • Thanks, Nice one! what would lets say, TaxonomyWebTaggingControl1.Text give me? Somthing like (label | guid)? – George Norberg Apr 14 '15 at 14:07
  • 1
    Label/GUID pairs that represent a Term object that is displayed in the control. TaxonomyWebTaggingControl.Text property – Arsalan Adam Khatri Apr 14 '15 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.