Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am working with a predefined set of Taxonomy data.

When I create a list item, I'm doing this based on a certain content type. I also want to apply taxonomy fields to this list item.

I'm using SP2010 clientContext object model.

Can someone post some sample code to demonstrate how it is possible to apply the taxonomy (meta data) field to the list item?

Update:

To ask another way, Do I apply the taxonomy field type to the list definition, the content type like any other field? Instead type would be "TaxonomyFieldType". Or do I manually add them on the fly to the list items as I go?

share|improve this question
    
Does the list have the taxonomy column already there? Are you wanting to add this column to the list, or you asking about adding data into that column when adding a listitem? –  James Love Dec 15 '10 at 10:51
    
@James, hi - I would like to include it at the content type level. I think I figured out 1/2 of the problem. It seems to be a type of siteColumn, so you can treat it as you would any other column once you have it setup in the content type right? –  user879 Dec 15 '10 at 16:33

1 Answer 1

up vote 1 down vote accepted

This method uses object model (client context might work but untested)

using (SPSite spSite = new SPSite(this.siteURL))
{
  SPList oList = spSite.RootWeb.Lists[ListName]; 
  SPListItem oListItem = oList.AddItem();

 // Example of normal field 
 oListItem["Title"] = SomeStringTitle;

SPFieldCollection fields = spSite.RootWeb.Lists[oList.ID].Fields;
TaxonomyField fieldToSet = fields.GetFieldByInternalName(StringFieldName) as TaxonomyField ; 

Term term = // You should find the term as required in your function...

fieldToSet.SetFieldValue(oListItem, term);

// Don't forget to update
oListItem.Update();

I use the SetFieldValue (which has some overloads) because you don't need to deal with hidden list this way, it gets the WssID for you (not sure how, but it works).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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