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.

1
  • What you mean by have metadata field? You want the taxonomy selector control? Commented Feb 17, 2014 at 7:02

1 Answer 1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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